2

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]`);
1

3 Answers 3

4

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);

Sign up to request clarification or add additional context in comments.

2 Comments

Is 3.1 even? :)
@Joundill well it wasn't clearly specified whether there can be floats in the Array, but I updated my answer to not make this assumption. Although, it doesn't even make sense to be checking for the parity of an irrational number so I'm sure this won't have an effect
0

You can use .filter to do this with one line.

function removeEvens(numbers) {
    return numbers.filter(n => n % 2 !== 0);
}


const oddNumbers = removeEvens([1, 2, 3, 4, 5]);
console.log(oddNumbers, '<-- should equal [1, 3, 5]');

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.