I built a function in JavaScript called oddNumbers which I used a for loop and if condition in order to print the odd numbers between 0 to 10. However, the console printed only "Odd: 1".
I have traied to print odd numbers list from 0 to 10 using the following code:
function oddNumbers() {
for (let i = 0; i <= 10; i++) {
if ((i % 2) !== 0)
return console.log('Odd: ', i);
}
}
oddNumbers();
But, it only prints "Odd: 1" in the console. Could you please help me with this issue? I haven't found what can be the error or errors yet.
The return statement will exit your function.
change the code to:
function oddNumbers() {
for (let i = 0; i <= 10; i++) {
if ((i % 2) !== 0)
console.log('Odd: ', i);
}
}
oddNumbers();