Day 20 - JS loops
by Nino · 56 things on Twos
- Self-assigning Variables
- Self-assignment is when we set a variable to its own value
- let wallet = 3;
wallet = wallet;
console.log(wallet);
> 3
- let nameInfo = "Account name:";
nameInfo = nameInfo + " Elton";
nameInfo = nameInfo + " John";
console.log(nameInfo);
> Account name: Elton John
- self-assigning is such a powerful programming concept because it lets us track data that changes over time
- ---
- Assigning with Operators
- let likes = 5;
likes += 1;
console.log(likes);
> 6
- let likes = 5;
likes -= 3;
console.log(likes);
> 2
- let likes = 5;
likes++;
console.log(likes);
> 6
- let likes = 5;
likes--;
console.log(likes);
> 4
- Repeating Code with While Loops
- to build larger programs and websites, we repeat lines of code using a while loop
- while (true) {
console.log("and again");
}
> and again
and again
and again
...
- the code that a while loop repeats goes inside braces { }
- we call it the while loop's code block
- Stopping While Loops
- to stop a loop, we start by creating a variable outside of the loop
- we use the variable in the condition to decide whether or not the loop should run its code block
- for example, (keepGoing === true)
- inside the code block, we stop the loop by setting keepGoing to false so that the condition returns false
- let keepGoing = true;
while (keepGoing === true) {
console.log("and again");
keepGoing = false;
}
> and again
- the loop runs its entire code block because keepGoing is true at first, but doesn't run again if we set keepGoing to false
- Controlling While Loops
- to control the times a while loop repeats, we start with a variable set to a number. We call this variable a counter variable
- let counter = 1;
- then, we use a comparison in the condition to compare the counter variable to a number. In this case, counter < 4
- let counter = 1;
while (counter < 4) {}
- inside the code block, we make the condition return false and stop the loop by incrementing the counter variable
- let counter = 1;
while (counter < 4) {
console.log(counter);
counter++;
}
> 1
2
3
- changing the condition tells the loop when to stop
- let counter = 1;
while (counter < 10) {
console.log(counter);
counter++;
}
> 1
2
3
4
5
6
7
8
9
- changing the counter variable's value changes when the loop starts, like setting counter to 5
- let counter = 5;
while (counter < 10) {
console.log(counter);
counter++;
}
> 5
6
7
8
9
- since the whole code block runs, the order you write code affects what the console displays
- let counter = 5;
while (counter < 10) {
counter++;
console.log(counter);
}
> 6
7
8
9
10
- Repeating Code with For Loops
- using for loops, we can rewrite the same program making it easier for other programmers to understand
- for (let i = 0; i < 4; i++) {
console.log("**********---------");
}
for (let i = 0; i < 4; i++) {
console.log("-------------------");
}
> **********---------
**********---------
**********---------
**********---------
-------------------
-------------------
-------------------
-------------------
- to create a for loop, we start with the for keyword, parentheses ( ), and braces { }
- for () {}
- a common name for a counter variable is the letter i which stands for index
- for (let i = 1;) {}
- after the semicolon, we place a condition like i < 5 and add another semicolon ;
- for (let i = 1; i < 5;) {}
- last, we increment the counter variable with i++
- for (let i = 1; i < 5; i++) {
console.log(i);
}
> 1
2
3
4
- Looping Downward
- for (let i = 5; i > 0; i--) {
console.log(i);
}
> 5
4
3
2
1
- to include 0 when counting down, we use >= instead of >
- for (let i = 5; i >= 0; i--) {
console.log(i);
}
> 5
4
3
2
1
0