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

102
Views
I cannot find even and odd numbers in an array javascript

I know how to find an even or odd number but I can't seem to find any results for finding odd and even numbers in an array. I want to write even numbers in a paragraph in HTML and odd numbers in another paragraph.

let numbers = [1,2,3,4,5,6,7,8,9,10]

let e;
let no = numbers.forEach(sort());

function sort() {
  if(numbers[e] % 2 === 0) {
    console.log(`${numbers[e]} is even`);
    // document.querySelector("#even").innerHTML = numbers[e];
  } else {
     console.log(`${numbers[e]} is odd`);
     // document.querySelector("#odd").innerHTML = numbers[e];
  }
}

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

0

Pass sort to forEach, don't call that

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

function sort(e) {
  const type = e % 2 === 0 ? "even" : "odd";
  console.log(`${e} is ${type}`);
}
const no = numbers.forEach(sort);

about 4 years ago · Juan Pablo Isaza Report

0

Using a for loop and an if statement.

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

function sort() {
  for (let e = 0; e < numbers.length; e++) {
    if (numbers[e] % 2 === 0) {
      console.log(`${numbers[e]} is even`);
      // document.querySelector("#even").innerHTML = numbers[e];
    } else {
      console.log(`${numbers[e]} is odd`);
      // document.querySelector("#odd").innerHTML = numbers[e];
    }
  }
}

sort()

about 4 years ago · Juan Pablo Isaza Report

0

You can run it like below so if you wanna check it using the i which is key then remove comment part of if otherwise continue with the same.

let numbers = [1,2,3,4,5,6,7,8,9,10]

numbers.forEach((val, i) => sort(i, val));

function sort(i, val) {
  //if(numbers[i] % 2 === 0) {
  if(val % 2 === 0) {
    //console.log(`${numbers[i]} is even`);
    console.log(`${val} is even`);
  } else {
     //console.log(`${numbers[i]} is odd`);
     console.log(`${val} is 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!