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

Scorri per mostrare il menu

book
Challenge: Random Multiple of 7

Compito

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;

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 6. Capitolo 6

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

book
Challenge: Random Multiple of 7

Compito

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;

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 6. Capitolo 6
Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Siamo spiacenti che qualcosa sia andato storto. Cosa è successo?
some-alt