Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
Why the code provides the odd number from the array?

could you please help me understand why the following line result is [1,3]?

[1,3,6].filter( item => item % 2)

I was expecting to receive the even number from the array.

Many thanks!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Filter checks for a boolean, 3 % 2 is equal to 1, which evaluates to true.

You want item % 2 == 0

about 4 years ago · Juan Pablo Isaza Report

0

Even numbers are those numbers that are exactly divisible by 2.

The remainder operator % gives the remainder when used with a number. For example,

const number = 6;
const result = number % 4; // 2 

Hence, when % is used with 2, the number is even if the remainder is zero. Otherwise, the number is odd.

// program to check if the number is even or odd
// take input from the user
const number = prompt("Enter a number: ");

//check if the number is **even**
if(number % 2 == 0) {
    console.log("The number is even.");
}

// if the number is **odd**
else {
    console.log("The number is odd.");
}

ref : https://www.programiz.com/javascript/examples/even-odd

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!