Challenge: Random Multiple of 7
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) + 1generates a random integer between1and10.While understanding it isn't necessary for this task, here's a brief explanation:
Math.random()produces a random decimal between0(inclusive) and1(exclusive).- Multiplying by
10scales this value to a range between0and10(still excluding 10).Math.floor(value)rounds the number down to the nearest whole number, resulting in an integer from0to9.- Adding
1shifts the range from1to10, ensuring we never get0.
- Create a
do-whileloop which:- Prints the current value of
number; - Increments
numberby1; - Runs as long as
numberis not a multiple of7. Note that the code-block in ado-whileloop is executed before the condition check, therefore you need to subtract one fromnumber(number - 1) before checking it's value;
- Prints the current value of
Løsning
Tak for dine kommentarer!
single
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Fantastisk!
Completion rate forbedret til 1.33
Challenge: Random Multiple of 7
Stryg for at vise menuen
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) + 1generates a random integer between1and10.While understanding it isn't necessary for this task, here's a brief explanation:
Math.random()produces a random decimal between0(inclusive) and1(exclusive).- Multiplying by
10scales this value to a range between0and10(still excluding 10).Math.floor(value)rounds the number down to the nearest whole number, resulting in an integer from0to9.- Adding
1shifts the range from1to10, ensuring we never get0.
- Create a
do-whileloop which:- Prints the current value of
number; - Increments
numberby1; - Runs as long as
numberis not a multiple of7. Note that the code-block in ado-whileloop is executed before the condition check, therefore you need to subtract one fromnumber(number - 1) before checking it's value;
- Prints the current value of
Løsning
Tak for dine kommentarer!
single