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
Solução
Obrigado pelo seu feedback!
single
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Incrível!
Completion taxa melhorada para 1.33
Challenge: Random Multiple of 7
Deslize para mostrar o menu
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
Solução
Obrigado pelo seu feedback!
single