To get odd numbers in JavaScript, we can use a loop to iterate through a series of numbers and then use a conditional statement that checks if each number is odd. We can use the modulus operator %
to check if a number is divisible by 2, which means it's even. If a number is not divisible by 2, then it's odd.
Here's an example code snippet that prints out all the odd numbers from 1 to 10:
index.tsx101 chars6 lines
In this code, we use a for
loop to iterate from 1 to 10. We use the conditional statement if (i % 2 !== 0)
to check if each number i
is not divisible by 2, and therefore odd. Then we use console.log(i)
to print out each odd number to the console.
gistlibby LogSnag