JavaScript Loops Worksheet
Questions
Question 1
What is the difference between a while loop and a for loop?
for loops are for when you know how many times it needs to run and while loops are for running when only certain condiations are met
Question 2
What is an iteration?
repeting the same code block untill the condation to stop is met eg a set number of runs
Question 3
What is the meaning of the current element in a loop?
tells you the current vaule of loop item for that iteration
Question 4
What is a 'counter variable'?
more or less a counter for the number of iterations
Question 5
What does the break; statement do when used inside a loop?
when it hits the break it completely stops the loop and moves to the next part of the code
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);
}
counts all the h4 elements on the page
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.