I need to remove the even numbers for this array
function removeEvens(numbers) {
}
/* Do not modify code below this line */
const oddNumbers = removeEvens([1, 2, 3, 4, 5]);
console.log(oddNumbers, `<-- should equal [1, 3, 5]`);
I need to remove the even numbers for this array
function removeEvens(numbers) {
}
/* Do not modify code below this line */
const oddNumbers = removeEvens([1, 2, 3, 4, 5]);
console.log(oddNumbers, `<-- should equal [1, 3, 5]`);
The first step is how you check for evens - you can do this using the modulus operator (%
) to see if the remainder when divided by 2 is 0, meaning that the number is even. Next you can filter the array to include only the numbers which pass this test:
function removeEvens(numbers) {
return numbers.filter(n => n % 2 !== 0); // if a number is even, remove it
}
const oddNumbers = removeEvens([1, 2, 3, 4, 5]);
console.log(oddNumbers);
Here is the code:
function removeEvens(numbers) {
return numbers.filter(n => n % 2 == 1);
}
/* Do not modify code below this line */
const oddNumbers = removeEvens([1, 2, 3, 4, 5]);
console.log(oddNumbers);