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

Svep för att visa menyn

book
Challenge: Random Multiple of 7

Uppgift

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;

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 6. Kapitel 6

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

book
Challenge: Random Multiple of 7

Uppgift

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;

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 6. Kapitel 6
Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Vi beklagar att något gick fel. Vad hände?
some-alt