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

Veeg om het menu te tonen

book
Challenge: Random Multiple of 7

Taak

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;

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 6. Hoofdstuk 6
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?

Vraag AI

expand
ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

book
Challenge: Random Multiple of 7

Taak

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;

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 6. Hoofdstuk 6
Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt