JavaScript Loops Worksheet

Questions

Question 1

What is the difference between a while loop and a for loop?

The main thing that distinguishes while loops from the next type of loop that we'll discuss is that when writing the code, you don't know how many times the loop will repeat. In the last example ,the loop will repeat until the user enters the correct number.

Question 2

What is an iteration?

looping is also called iterating. And each time the code in the loop body executes is said to be an iteration.

Question 3

What is the meaning of the current element in a loop?

When looping through an array, the current element is the one that is processed within the body of the loop. This will change with each iteration of the loop.

Question 4

What is a 'counter variable'?

A counter variable is declared and initialized to a value of 1. You can use any variable name you want for the counter variable, but it's common for programmers to simply use x or i.

Question 5

What does the break; statement do when used inside a loop?

There are times when you want to terminate a loop before it completes all of its iterations. To do this, you use a break statement.

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.