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 between1
and10
.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
10
scales this value to a range between0
and10
(still excluding 10).Math.floor(value)
rounds the number down to the nearest whole number, resulting in an integer from0
to9
.- Adding
1
shifts the range from1
to10
, ensuring we never get0
.
- Create a
do-while
loop which:- Prints the current value of
number
; - Increments
number
by1
; - Runs as long as
number
is not a multiple of7
. Note that the code-block in ado-while
loop 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
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 6. Kapitel 6