JavaScript Loops Worksheet
Questions
Question 1
What is the difference between a while loop and a for loop?
while loops go as long as a variable is true, let loops go as long as an operation is true, typically used to loop a specified amount of times
Question 2
What is an iteration?
The instance a loop is on
Question 3
What is the meaning of the current element in a loop?
The index the array is on
Question 4
What is a 'counter variable'?
the variable used to track iterations
Question 5
What does the break; statement do when used inside a loop?
it ends the loop
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);
}
The code is grabbing the text of each h4 element
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.