Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

find average

const arr = [2, 4, 5, 6];

// Find average function
function fAve(totalVal, arrVal, index, array) {
	const val = totalVal + arrVal;
    
    if (index === array.lengh -1) {
    	return val / array.length;
    }
    
    return val;
}

// Pass function into reduce method
const average = arr.reduce(fAve);

console.log(average);


//node (js file name).
Source by dev.to #
 
PREVIOUS NEXT
Tagged: #find #average
ADD COMMENT
Topic
Name
4+4 =