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
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Fantastiskt!
Completion betyg förbättrat till 1.33
Challenge: Random Multiple of 7
Svep för att visa menyn
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
Tack för dina kommentarer!
single