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

Glissez pour afficher le menu

book
Challenge: Random Multiple of 7

Tâche

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;

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 6. Chapitre 6
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?

Demandez à l'IA

expand
ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

book
Challenge: Random Multiple of 7

Tâche

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;

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 6. Chapitre 6
Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?
some-alt