Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Random Multiple of 7 | Discovering Loops
Introduction to JavaScript

Pyyhkäise näyttääksesi valikon

book
Challenge: Random Multiple of 7

Tehtävä

Swipe to start coding

Use a do-while loop to print numbers starting from a random number (represented by the variable number).

The loop should continue printing and increasing the number by 1 until the number is a multiple of 7.

Note

The expression Math.floor(Math.random() * 10) + 1 generates a random integer between 1 and 10.

While understanding it isn't necessary for this task, here's a brief explanation:

  • Math.random() produces a random decimal between 0 (inclusive) and 1 (exclusive).
  • Multiplying by 10 scales this value to a range between 0 and 10 (still excluding 10).
  • Math.floor(value) rounds the number down to the nearest whole number, resulting in an integer from 0 to 9.
  • Adding 1 shifts the range from 1 to 10, ensuring we never get 0.
  • Create a do-while loop which:
    • Prints the current value of number;
    • Increments number by 1;
    • Runs as long as number is not a multiple of 7. Note that the code-block in a do-while loop is executed before the condition check, therefore you need to subtract one from number (number - 1) before checking it's value;

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 6. Luku 6

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

book
Challenge: Random Multiple of 7

Tehtävä

Swipe to start coding

Use a do-while loop to print numbers starting from a random number (represented by the variable number).

The loop should continue printing and increasing the number by 1 until the number is a multiple of 7.

Note

The expression Math.floor(Math.random() * 10) + 1 generates a random integer between 1 and 10.

While understanding it isn't necessary for this task, here's a brief explanation:

  • Math.random() produces a random decimal between 0 (inclusive) and 1 (exclusive).
  • Multiplying by 10 scales this value to a range between 0 and 10 (still excluding 10).
  • Math.floor(value) rounds the number down to the nearest whole number, resulting in an integer from 0 to 9.
  • Adding 1 shifts the range from 1 to 10, ensuring we never get 0.
  • Create a do-while loop which:
    • Prints the current value of number;
    • Increments number by 1;
    • Runs as long as number is not a multiple of 7. Note that the code-block in a do-while loop is executed before the condition check, therefore you need to subtract one from number (number - 1) before checking it's value;

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 6. Luku 6
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt