I am trying to create a function evenoddarray() which is supposed to accept one parameter which should be an array of numbers. This function should check all the numbers in the array in order to log whether each number is even or odd.
For example, evenoddarray(1, 45,8,6,9) should display ‘odd,odd,even,even,odd’.
Thank you.
function evenoddarray(array) {
let tab = [1, 45,8,6,9];
for (const value of tab) {
if (value % 2 === 0) {
console.log('even');
}
else {
console.log('odd');
}
}
}
evenoddarray();
>Solution :
You just need to capture the results as you loop through your tabarray. There are many shorter versions but this may make more sense at first.
function evenoddarray() {
let tab = [1, 45,8,6,9];
let result = [];
for (const value of tab) {
if (value % 2 === 0) {
result.push('even');
}
else {
result.push('odd');
}
}
return result;
}
console.log(evenoddarray().toString());
console.log(evenoddarray().join(','));