I've a task to write function that returns the average value of even numbers from an array. If there are no even numbers in the array, return null. Here are arrays and expected result: [1,2,3,4,5,6,7] - should return "4" and [1,1,1,1] - should return "null". Where shall I put condition for null?I've tried different options but nothing works but maybe my code is wrong.
function getEvenAverage(array) {
const even = array.filter(function (element) {
return element % 2 === 0
});
const sum = even.reduce(function (sum, element) {
return (sum element);
});
return sum / even.length;
}
const result1 = getEvenAverage([1,2,3,4,5,6,7])
console.log(result1);
const result2 = getEvenAverage([1,1,1,1])
console.log(result2);
CodePudding user response:
You can achieve this in 1 loop as well:
Logic:
- Create an object(say
map
) to hold values - Loop on array.
- Check if current item is even, compute total and length. Set it to
map
- At the end, check for length and return value
function getEvenAverage(array) {
const map = array.reduce((acc, item) => {
if (item % 2 === 0) {
return { total: acc.total item, length: acc.length 1 }
}
return acc
}, { total: 0, length: 0 });
return map.length ? map.total / map.length : null
}
const result1 = getEvenAverage([1, 2, 3, 4, 5, 6, 7])
console.log(result1);
const result2 = getEvenAverage([1, 1, 1, 1])
console.log(result2);
CodePudding user response:
check this code
function getEvenAverage (arr) {
let temp = [];
let sum = 0;
let count = 0;
arr.map(item => {
if(item % 2 === 0) {
temp = [...temp, item]
}
})
temp.map(item => {
sum = item;
count =1;
})
if(temp.length ===0) {
return null
}
return sum/count;
}
CodePudding user response:
You need to do the following changes:
- Add
null
as an initial value for Array.prototype.reduce - Check if
sum === null
returnsum
, otherwise calculate an average value
You can find the code with changed applied below
function getEvenAverage(array) {
const even = array.filter(function (element) {
return element % 2 === 0
});
const sum = even.reduce(function (sum, element) {
return (sum element);
}, null);
return sum === null ? sum : sum / even.length;
}
const result1 = getEvenAverage([1,2,3,4,5,6,7])
console.log(result1);
const result2 = getEvenAverage([1,1,1,1])
console.log(result2);
CodePudding user response:
Originally posted as a simple edit to this answer. Reposting as a separate answer below (as the other answer-er, Rajesh rolled back & noted that this be posted separately).
Added comments as this is now posted as a separate answer.
// method to get average of even numbers in array
const getEvenAverage = (arr) => (
arr // use optional-chainging "?." to handle bad "arr" values
?.reduce( // iterate over the array
(acc, itm, idx) => { // access "acc" (accumulator), "itm" and "idx"
if (itm % 2 === 0) { // if itm is even number
acc.tot = itm; // add it to "acc" total (ie, "tot" prop)
acc.len ; // increment the "len" prop of "acc"
};
if ( // if processing last "itm"
idx === arr.length - 1 &&
acc.len > 0 // and have previously processed even number "itm"
) { // update "result" prop of "acc"
acc.result = acc.tot / acc.len;
};
return acc; // return the updated "acc" for each iteration
}, // initialize "acc" to have three props
{ tot: 0, len: 0, result: null }
) // simply send the "result" back to the caller
?.result
);
console.log(
'call fn with array: 1,2...7\naverage-of-even-numbers: ',
getEvenAverage([1, 2, 3, 4, 5, 6, 7])
);
console.log(
'call fn with array: 1,1,1,1\naverage-of-even-numbers: ',
getEvenAverage([1, 1, 1, 1])
);
console.log(
'call fn with empty-array: []\naverage-of-even-numbers: ',
getEvenAverage([])
);
console.log(
'call fn with undefined: \naverage-of-even-numbers: ',
getEvenAverage()
);
.as-console-wrapper { max-height: 100% !important; top: 0 }