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);
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]');
Here is the code. Hope this will be helpful!
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<script>
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);
</script>
</body>
</html>