How to resolve the algorithm Loops/Break step by step in the JavaScript programming language
How to resolve the algorithm Loops/Break step by step in the JavaScript programming language
Table of Contents
Problem Statement
Show a loop which prints random numbers (each number newly generated each loop) from 0 to 19 (inclusive).
If a number is 10, stop the loop after printing it, and do not generate any further numbers.
Otherwise, generate and print a second random number before restarting the loop.
If the number 10 is never generated as the first number in a loop, loop forever.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Loops/Break step by step in the JavaScript programming language
for Loop with Break:
for (;;) {
// Generate a random number between 0 and 19
var a = Math.floor(Math.random() * 20);
// Print the number
print(a);
// If the number is 10, break out of the loop
if (a == 10)
break;
// Generate another random number
a = Math.floor(Math.random() * 20);
// Print the number, even though it's not 10
print(a);
}
Explanation:
This loop will continue generating random numbers and printing them indefinitely until it generates the number 10. Once 10 is generated, the loop will break. Note that the variable a is declared inside the loop, so it's local to each iteration.
Recursive Function:
(function streamTillInitialTen() {
// Generate a random number between 0 and 19
var nFirst = Math.floor(Math.random() * 20);
// Print the number
console.log(nFirst);
// If the number is 10, return true
if (nFirst === 10) return true;
// Print another random number
console.log(
Math.floor(Math.random() * 20)
);
// Recursively call the function
return streamTillInitialTen();
})();
Explanation:
This recursive function will continue generating random numbers and printing them until it generates the number 10. When 10 is generated, it will return true. If any other number is generated, it will print that number and recursively call itself.
Recursive Function with Accumulator:
console.log(
(function streamTillInitialTen() {
// Generate a random number between 0 and 19
var nFirst = Math.floor(Math.random() * 20);
// If the number is 10, return an array with 10
if (nFirst === 10) return [10];
// Return an array with the first number and a second random number
return [
nFirst,
Math.floor(Math.random() * 20)
].concat(
streamTillInitialTen()
);
})().join('\n')
);
Explanation:
Similar to the previous recursive function, this function will continue generating random numbers and printing them, but it accumulates the numbers in an array. When 10 is generated, it returns an array with only 10. For all other numbers, it returns an array with the first number, a second random number, and the result of the recursive call. The join('\n') method is used to convert the array into a string with line breaks.
Source code in the javascript programming language
for (;;) {
var a = Math.floor(Math.random() * 20);
print(a);
if (a == 10)
break;
a = Math.floor(Math.random() * 20);
print(a);
}
(function streamTillInitialTen() {
var nFirst = Math.floor(Math.random() * 20);
console.log(nFirst);
if (nFirst === 10) return true;
console.log(
Math.floor(Math.random() * 20)
);
return streamTillInitialTen();
})();
console.log(
(function streamTillInitialTen() {
var nFirst = Math.floor(Math.random() * 20);
if (nFirst === 10) return [10];
return [
nFirst,
Math.floor(Math.random() * 20)
].concat(
streamTillInitialTen()
);
})().join('\n')
);
You may also check:How to resolve the algorithm Ranking methods step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Show ASCII table step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Magic squares of odd order step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Pig the dice game step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Shell one-liner step by step in the JavaScript programming language