JavaScript Loops Worksheet

Questions

Question 1

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

A while loop can run as long as its condition stays true. You can control how many times a for loop runs but adding a condition.

Question 2

What is an iteration?

An iteration is one complete cycle of a loop.

Question 3

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

The current element is the value that the loop is working with at that moment.

Question 4

What is a 'counter variable'?

A counter variable is used to keep track of how many times a loop has run.

Question 5

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

The top break statement stops the loop even if the loops normal ending condition has not been reached.

Question 6

Explain what the following code is doing (if you would like to run this code, you can paste it into the SCRIPT element in the page):


		const allH4elements = document.getElementsByTagName("h4");
		for(let x = 0; x < allH4elements.length; x++){
			console.log(allH4elements[x].innerHTML);
		}
		
It logs the text of every h4 element on the page to the console.

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.