简体   繁体   中英

Odd and Even number using single loop

output should be: 1 3 5 7 9 11 etc... 0 2 4 6 8 10 12 etc...

I don't know you need this or not...

 const odd =[]; const even = []; for (var x=0; x<=15; x++) { if (x === 0 || x % 2 === 0) { even.push(x); } else { odd.push(x); } } console.log(odd); console.log(even);

This is a way to do it, but please next time try to solve it by yourself first.

 const n = 100; const odd = []; const even = []; for (let i = 0; i < n; ++i) { (i % 2 === 0? even: odd).push(i); } console.log({ odd, even });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM