-
Notifications
You must be signed in to change notification settings - Fork 0
/
script-advanced.js
135 lines (110 loc) · 2.6 KB
/
script-advanced.js
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
126
127
128
129
130
131
132
133
134
135
console.log("Howdy! Welcome to the Control Flow script");
/*
* Conditional Statements
* ===========================
*/
let amIHappy = false;
let doIKnowIt = true;
// if statements with no else
//---------------------------
if (amIHappy === true) {
console.log("clap your hands");
console.log("stomp your feet");
}
if (amIHappy === true && doIKnowIt === true) {
console.log("I'm happy and I know it");
}
// if..else statements
//---------------------------
let age = 18;
let alive = true;
if (age >= 18) {
console.log("I vote that Mark should be fired immediatly");
} else {
console.log("Live in the blissful ignorance of youth");
}
// nested if..else
//---------------------------
if (alive === true) {
if (age >= 18) {
console.log("I vote that Mark should be fired immediatly");
} else {
console.log("Live in the blissful ignorance of youth");
}
} else {
console.log("You dead, what are you doing trying to vote???");
}
// if..else if..else statements
//---------------------------
let hour = 18;
let action = "code";
if (hour < 12) {
action = "drink coffee and code";
} else if (hour < 17) {
action = "code and cry";
} else {
action = "code and cry alot, alot";
}
console.log(action);
// switch statement
//---------------------------
let food = "potato";
switch (food) {
case "carrot":
console.log("chomp chomp");
break;
case "potato":
console.log("boil it, mash it, stick it in a stew");
break;
default:
console.log("eat it");
}
/*
* Loops
* ===========================
*/
// for loops
//---------------------------
for (let i = 0; i < 130; i++) {
console.log(i);
}
console.log("next loop");
for (let i = 9; i >= 0; i--) {
console.log(i);
}
console.log("next loop");
for (let i = 0; i < 10; i += 2) {
console.log(i);
}
// for loop to iterate(loop) through an array
// -----------------------------------------
let dogs = ["lab", "pitbull", "terrier", "golden", "cutie"];
console.log(dogs.length);
for (let i = 0; i < dogs.length; i++) {
console.log(dogs[i]);
}
// for loop search for product
//-------------------------------------------
let products = ["toothbrush", "backpack", "taco", "pencil"];
let tacoFound = false;
for (let i = 0; i < products.length; i++) {
if (products[i] === "taco") {
tacoFound = true;
break;
}
}
console.log("tacoFound: ", tacoFound);
// while loops
//-------------------------------------------
let i = 0;
while (i < 10) {
console.log(i);
i++;
}
let number = Math.floor(Math.random() * 100);
let timesRun = 0;
while (number !== 50) {
number = Math.floor(Math.random() * 100);
timesRun++;
}
console.log(timesRun);