thejsway / thejsway

The JavaScript Way book

Home Page:https://thejsway.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

An ambiguity in the chapter04.md

vsemozhetbyt opened this issue · comments

Improve the program so that it also shows odd numbers. Improve it again so that the initial number is given by the user.

This program must show exactly 10 numbers including the first one, not 11 numbers!

If the initial number is given by the user, could this program show less than 10 numbers?

No, it should always display 10 numbers.

@bpesquet Sorry, I cannot understand. If a user sets the initial number as 9, how this program can output 10 odd/even numbers between 9 and 10?

OK, the sentence is hopefully more clear now.

@bpesquet
This is the second possible variant:

for (let i = 1; i <= 10; i++) {
  if (i % 2 === 0) {
    console.log(`${i} is even`);
  } else {
    console.log(`${i} is odd`);
  }
}

output:

1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even

This is the third possible variant with a user input:

for (let i = Number(prompt("Type the initial number:")); i <= 10; i++) {
  if (i % 2 === 0) {
    console.log(`${i} is even`);
  } else {
    console.log(`${i} is odd`);
  }
}

Output if a user types 9:

9 is odd
10 is even

As you can see, the last program cannot always show exactly 10 numbers.