JavaScript Loops Worksheet
Questions
Question 1
What is the difference between a while loop and a for loop?
A while loop will continue to repeat as long as a boolean expression evaluates to true.
A for loop allows you to execute a code block a specific number of times. These include a counter variable, which is used to determine how many times the loop iterates.
A for loop allows you to execute a code block a specific number of times. These include a counter variable, which is used to determine how many times the loop iterates.
Question 2
What is an iteration?
To iterate means to loop;
an iteration is one full loop of a process that is repeatedly executed until the conditions have been met.
Question 3
What is the meaning of the current element in a loop?
The current element is the element from an array that is currently being processed within the body of the loop.
With each iteration of the loop, the current element will change.
Question 4
What is a 'counter variable'?
A counter variable is used in a For Loop to determine how many times the loop iterates.
For example: for(let x = 1; x < 5; x++){body of the loop}
For example: for(let x = 1; x < 5; x++){body of the loop}
Question 5
What does the break; statement do when used inside a loop?
The break; statement is used when you want to terminate a loop before it completes all of its iterations.
Once the loop finds what it is looking for, the loop will terminate, instead of proceeding to complete all iterations.
Coding Problems
Coding Problems - See the 'script' tag below this h3 tag. You will have to write some JavaScript code in it.
Always test your work! Check the console log to make sure there are no errors.