JavaScript Loops Worksheet

Questions

Question 1

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

A for loop is best when you know exactly how many times you want to repeat something. It’s like saying, “I’ll do this 10 times.” You set everything up (starting point, end point, and how you’ll get there) all in one line, making it straightforward for specific, counted tasks.

A while loop, on the other hand, is great when you’re not sure how many times you’ll need to repeat something. It’s more like saying, “Keep doing this until a certain condition is met.” It just keeps going as long as the condition remains true, which makes it very flexible.

Question 2

What is an iteration?

An iteration is just one round of doing something in a loop. Think of it like steps in a dance: each step is an iteration. When you loop through something multiple times, each time you go through it, that’s one iteration.

Question 3

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

The current element in a loop refers to the specific item or value the loop is focused on at that moment in a list, array, or any collection of items..

Question 4

What is a 'counter variable'?

A counter variable is just a way to keep track of how many times a loop has run. It starts at a certain number, usually 0 or 1, and goes up by 1 each time the loop repeats. It’s like counting on your fingers every time you do something in a loop.

Question 5

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

The break; statement is like a way of saying, “Stop the loop right here!”

When a loop hits break, it stops running immediately and skips to whatever comes next after the loop. It’s useful if you’re looking for something specific in a loop and want to quit as soon as you find it.

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.