xxxxxxxxxx
int a, b, c, d, sum, average;
cout << "Input the 1st number: ";
cin >> a;
cout << "Input the 2nd number: ";
cin >> b;
cout << "Input the 3rd number: ";
cin >> c;
cout << "Input the 4th number: ";
cin >> d;
sum = a+b+c+d;
average = sum/4;
cout << "The average is: " << average << endl;
xxxxxxxxxx
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).