Part 4- A Smarter Way to Learn JavaScript Flashcards
In this piece of code, what does: var i = 0 mean?
for (var i = 0; i<=4; i++) {
//code to run goes here
}
It’s the “counter”. It changes in each iteration so that eventually the FOR loop will end.
In this piece of code, what does i<=4 mean?
for (var 1 = 0; i<=4; i++) {
//code to run goes here
}
*it’s the “loop limiter”
*it tells you how many iterations will run.
In this piece of code, what does i++ mean?
for (var 1 = 0; i<=4; i++) {
//code to run goes here
}
*it’s the “incrementor” (my name)
*it adjusts the variable i for the next iteration
What’s the format of the first line of a FOR loop?
for (var i = 0; i<=4; i++) {
How would you code to loop through a test repeatedly?
FOR loop with an embedded IF loop:
for (var 1 = 0; i<=4; i++) {
if (x === y) {
//do this code
}
}
What’s the point of a FOR loop? What does it do?
It repeats the same code over and over, each time with a different value.
This is looping, AKA iteration.
What if you want to FOR loop your way through each value in an array, but you don’t know how long the array is?
*set the second part of a FOR loop to this: i < arrayName.length.
*Or you can set up a variable equal to the length of the array:
var numElements = arryName.length, then you put it in the “i <” spot (second part of a FOR loop):
for (var i = 0; i < numElements; i++)
How do you set up a variable equal to the length of an array?
var x = arrayName.length;
Do Boolean use quotation marks like a string?
No. It’s just true and false
What is a flag and how do you use it in looping?
*a flag is a variable that starts as one value, then changes value if something happens.
*in a FOR loop, you might set a flag to a Boolean value so that when the loop matches something, it returns the other Boolean value
What’s a BREAK
break;
A keyword that ends your loop. Put it in your code so if you get the right answer, it doesn’t keep looping and looping.
What is an assignment operator?
= += -=
*= /= %= :
*An assignment operator is the code used to assign a new value to a variable, property, event, etc.
Can you nest FOR loops?
Yes. You can use a j for the first nested loop, k for the next, etc etc.
Think of the nested loop as the second hand and the original loop as the minute hand. The nested loop will go through all of its iterations, then the original loop will increment its variable. Then the nested loop will go through all its iterations again, etc etc
What are the two types of control flow statements in JavaScript?
*Conditional
*Iterative (Looping)
Change letters to lowercase?
var str1 = str1.toLowerCase();