Home > Blockchain >  Javascript - odd and even numbers can't work synchronous on my code?
Javascript - odd and even numbers can't work synchronous on my code?

Time:12-06

let num = prompt("Number");
for (let i = 0; i < num; num--) {
  if (num % 2 == 0) {
    console.log(num);
  }
}

// ******* not running ??? ***********
num = "";
for (let i = 0; i < num; num--) {
  if (num % 2 !== 0) {
    console.log(num);
  }
}

my objective is:1 . to enter a number in prompt 2.to show all even and odd numbers up to the entered number. idk. why second part !== is not working

for the ideal mode I was wondering if it is possible to enter a loop in array.

to have something like that:

Prompt (for exemple:50) evenNumber = [ 2, 4, 6, 8, 10... ] oddNumber = [1, 3, 5, 7, 9...]

CodePudding user response:

Here is a short way of doing it:

function evenOdd(n){
 const evenodd=[[],[]];
 for (let i=0; i<n; i  ) evenodd[i%2].push(i)
 return evenodd;
}

const [even,odd]=evenOdd(prompt("Generate even and odd numbers up to:"));

console.log("even numbers:",even);
console.log("odd numbers:",odd);

  • Related