Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript – odd and even numbers can't work synchronous on my code?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading