-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathloops.js
More file actions
129 lines (79 loc) · 3.49 KB
/
loops.js
File metadata and controls
129 lines (79 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"use strict";
// OBJECTIVE ////////////////////////////////////////////////////////////////////////////////////////////////
// Study Loops, how they work, what use cases do they have, etc.
// SUMMARY //////////////////////////////////////////////////////////////////////////////////////////////////
// What is the problem?:
// Study loops
// What is the BEST solution?
// Find the most efficient way to use a loop.
// What are the special components of these solutions?:
// 1. "for" statment -- meant to iterate while incrementing or decrementing.
// example: [counts up]
// for (let i=0; i <= countTo; i++) {code looped through}
// example: [counts down]
// for (let i = countFrom; i >= 0; i-- ) {code looped through}
// 2. "for... of" -- creates a loop through iterable objects like arrays, maps, etc.
// 3. "for... in" -- meant to enumerate through object properties.
// What needs work?
// 1. Add more loop examples (i.e for...in, etc.) from research.
// 2. Add use case scenarios.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// STANDARD FOR LOOP UP (INCREMENTING) //////////////////////////////////////////////
function countUp() {
const countTo = 10;
for (let i=0; i <= countTo; i++) {
console.log(i);
}
}
console.log("FOR loop incrementing UP")
console.log(countUp());
///// STANDARD FOR LOOP DOWN (DECREMENTING) //////////////////////////////////////////////
function countDown() {
const countFrom = 10;
for (let i = countFrom; i >= 0; i-- ) {
console.log(i);
}
}
console.log("FOR loop decrementing DOWN")
console.log(countDown());
///// LOOP USING THE forEach() METHOD ////////////////////////////////////////////////////
function arrayLoop () {
const myArray = ["Tom", "Dick", "Harry"];
return myArray.forEach((element, index) => console.log(`the element ${index} is ${element}`));
}
console.log("FOR Loop iterating through an array");
console.log(arrayLoop());
///// FOR...IN... LOOPING THROUGH AN OBJECT ///////////////////////////////////////////////////////////
const myObject = {
fruit: "apple",
drink: "water",
desert: "cookie",
}
for (let key in myObject) {
console.log(`the key ${key} is ${myObject[key]}`);
}
var object = { "key1" : "one", "key2" : "two", "key3" : "three" },
key = "";
for (key in object) {
console.log(key); // "key1", "key2", "key3"
console.log( object[key] ); // "one", "two", "three"
}
///// LOOPING THROUGH AN OBJECT USING OBJECT.KEYS ////////////////////////////////////////////////////
const mySecondObject = {
hat: "baseball cap",
shirt: "long sleeves",
pants: "jeans",
}
Object.keys(mySecondObject)
// for (let{variable of declaration} character{temporary variable that is redeclared EVERY time through the loop} of string{iterable object we want to iterate through})
// so we'll iterate through every character of string one by one and set each character equal to the temporary variable "character".
// we then take that character and add it on to the start of the string reversed
// then after the entire for loop, we return reversed
function tester(string) {
let reversed = ''; // string that will be assembled over time
for (let character of string) {
reversed = character + reversed;
}
return reversed;
}
console.log(tester('this is a test'));