[해결로 표시] JavaScript의 홀수 숫자 1부터 N까지

  

4
주제 스타터

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].

예시:

입력:
5

출력:
1
3
5


입력:
4

출력:
1
3


입력:
7

출력:
1
3
5
7

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

1개 답글
2

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);
공유: