Skip to content

Commit f1d9aee

Browse files
committed
Section 6: Intro loops, For loops & Inifinite loops
1 parent 7b811db commit f1d9aee

5 files changed

Lines changed: 129 additions & 7 deletions

File tree

06-Objects-The-Core-of-Javascript/01/01.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ console.log(palette);
217217
// ******************************
218218
// Array Object Equality ******************************
219219
// Example 9
220-
220+
/*
221221
let nums = [1, 2, 3];
222222
let mystery = [1, 2, 3];
223223
@@ -263,4 +263,4 @@ console.log(data1 === data2); // false
263263
let compareObject = data2;
264264
console.log(data1 === compareObject); // false
265265
let data3 = data1;
266-
console.log(data3 === data1); // true
266+
console.log(data3 === data1); // true */

06-Objects-The-Core-of-Javascript/README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,4 @@ So the next thing that it's a little trickier is trying to check if an array con
221221

222222
So if i wanted to know if one array contained exactly the numbers one, two and three in that order, unfortunately there's not an easy option for us right now until we talk about loops. We don't really have a way of checking for array equality of values, what we will need to do is manually compare every element in an array to another array.
223223

224-
**The exact same thing holds true for objects.**
225-
226-
```
227-
228-
```
224+
**The exact same thing holds true for objects.**

07-The-World-of-Loops/01/01.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Test the JS script before starting
2+
// alert("It's working!");
3+
4+
// ******************************
5+
// For Loops ******************************
6+
// Example 1
7+
/*
8+
for (let i = 1; i <= 3; i++) {
9+
console.log('Hello: ', i)
10+
}
11+
12+
console.log('\n')
13+
for (let i = 1; i <= 10; i += 3) {
14+
console.log('i +=3: ', i)
15+
}
16+
17+
console.log('\n')
18+
for (let i = 1; i <= 32; i *= 2) {
19+
console.log('i *= 2: ', i)
20+
}
21+
22+
console.log('\n')
23+
for (num = 1; num <= 5; num++) {
24+
console.log(`${num}x${num} =`, num * num)
25+
console.log(`${num}x${num} = ${num * num}`)
26+
} */
27+
/*
28+
// Example 2
29+
console.log('\n')
30+
for (let i = 200; i >= 0; i -= 25) {
31+
console.log(i)
32+
}
33+
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>JavaScript</title>
8+
</head>
9+
<body>
10+
11+
<!-- source attribute which corresponds to the path to our file -->
12+
<script src='01.js'></script>
13+
</html>

07-The-World-of-Loops/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## 07-The-World-of-Loops
2+
3+
## 1. Intro to Loops
4+
5+
### JS LOOPS Repeating Code
6+
7+
### GOALS
8+
9+
- Write for loops
10+
- Write while loops
11+
- Avoid infinite loops!
12+
- Iterate over arrays and objects
13+
14+
### LOOPS
15+
16+
Doing things repeatedly
17+
18+
- Loops allow us to repeat code
19+
- "Print 'hello' 10 times
20+
- Sum all numbers in an array
21+
- There are multiple types:
22+
- for loop
23+
- while loop
24+
- for...of loop
25+
- for...in loop
26+
27+
## 2. For Loops
28+
29+
This is the syntax of a For Loop
30+
31+
```
32+
for (
33+
[initialExpression]; // initial value
34+
35+
[condition]; // when to run the loop
36+
37+
[incrementExpression] // how to change value each time
38+
)
39+
```
40+
41+
```
42+
for (let i = 200; i <= 0; i -= 25) {
43+
console.log('DOES IT WORK?');
44+
}
45+
```
46+
47+
### ANOTHER EXAMPLE
48+
49+
```
50+
// Start at 50
51+
// Stop at 0, Keep going as long as i>= 0
52+
// Subtract 10 on each iteration
53+
for (let i = 50; i >= 0; i -= 10) {
54+
console.log(i);
55+
// 50, 40, 30, 20 ,10, 0
56+
}
57+
```
58+
59+
## 3. Infinite Loops
60+
61+
You can write infinite loops and an infinite loop is something you absolutely want to avoid.
62+
63+
The idea behind the infinite loop is that you write a loop where the ending condition is never met.
64+
65+
```
66+
// DO NOT RUN THIS CODE!
67+
for (let i = 20; i >= 0; i++) {
68+
console.log(i);
69+
} // BAD!!!
70+
71+
// Or this infinite loop
72+
for(let i = 1; i !== 20; i+= 2) {
73+
console.log('Infinite Loop')
74+
}
75+
```
76+
77+
- Make sure you're going to the right direction
78+
- Your logic makes sense here
79+
- Generally try to avoid equality and non equality there
80+
- Prefer to use greater > than or < less than when i can

0 commit comments

Comments
 (0)