settingsAccountsettings
Menusettings

Q: Odd Numbers 1 to N in JavaScript

+2 votes

Write a JS function that reads an integer n and prints all odd numbers from 1 to n.

The input comes as a single number n. The number n will be an integer in the range [1 … 100 000].

Examples:

Input:
5

Output:
1
3
5


Input:
4

Output:
1
3


Input:
7

Output:
1
3
5
7

The output should hold the expected odd numbers, each at a separate line.

asked in JavaScript category by user icabe

1 Answer

+1 vote

Here is the javascript solution to this task:

function oddNum(number) {
    for (let i = 1; i <= number; i++) {
        if (i % 2 != 0) {
            console.log(i);
        }
    }
}

oddNum(7);

//2nd solution:
function oddNumberCheck(input) {
    for (let i = 1; i <= input; i += 2) {
        console.log(i);
    }
}

oddNumberCheck(7);
answered by user richard8502
...