The question is:
"Add code to the functions func1 and func2 in the places marked "ADD CODE HERE" in order to achieve the desired console logs."
function filterArray(array, callback) {
const newArray = [];
for (let i = 0; i < array.length; i += 1) {
if (callback(array[i])) newArray.push(array[i]);
}
return newArray;
}
const arrOfNums = [1, 2, 3, 4, 5];
function func1(num) {
// ADD CODE HERE
}
function func2(num) {
// ADD CODE HERE
}
console.log(filterArray(arrOfNums, func1)); // should log: [2, 4]
console.log(filterArray(arrOfNums, func2)); // should log: [1, 3, 5]
In func1 I tried:
if (num % 2 === 0) {
num = num;
} else {
num = null;
}
In func2 I tried:
if (num % 2 !== 0) {
num = num;
} else {
num = null;
}
I am unsure how else to approach this problem. My solution did not work and the console.log calls both returned empty arrays...
Thank you in advance!
filterhave to return a boolean for each value.