-
Notifications
You must be signed in to change notification settings - Fork 0
/
Udacity_JavaScript_While-and-For_Loops.js
131 lines (118 loc) · 3.41 KB
/
Udacity_JavaScript_While-and-For_Loops.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
var x = 1;
while (x <= 20) {
if ((x % 3 === 0) && (x % 5 !== 0)) {
console.log("Julia");
x = x + 1;
continue;
} else if ((x % 3 !== 0) && (x % 5 === 0)) {
console.log("James");
x = x + 1;
continue;
} else if ((x % 3 === 0) && (x % 5 === 0)) {
console.log("JuliaJames");
x = x + 1;
continue;
} else {
console.log(x);
x = x + 1;
continue;
}
x = x + 1
}
/* from Udacity JavaScript introduction course */
/*
* Programming Quiz: 99 Bottles of Juice (4-2)
*
* Use the following `while` loop to write out the song "99 bottles of juice".
* Log the your lyrics to the console.
*
* Note
* - Each line of the lyrics needs to be logged to the same line.
* - The pluralization of the word "bottle" changes from "2 bottles" to "1 bottle" to "0 bottles".
*/
var num = 99;
while (num > 0) {
if (num === 2) {
console.log((num) + " bottles of juice on the wall! " +
(num) + " bottles of juice! Take one down, pass it around... " +
(num-1) + " bottle of juice on the wall!")
num = num - 1;
continue;
} else if (num === 1) {
console.log((num) + " bottle of juice on the wall! " +
(num) + " bottle of juice! Take one down, pass it around... " +
(num-1) + " bottles of juice on the wall!")
num = num - 1;
continue;
} else {
console.log((num) + " bottles of juice on the wall! " +
(num) + " bottles of juice! Take one down, pass it around... " +
(num-1) + " bottles of juice on the wall!")
num = num - 1;
continue;
}
// check value of num
// print lyrics using num
// don't forget to check pluralization on the last line!
// decrement num
}
/*
* Programming Quiz: Countdown, Liftoff! (4-3)
*
* Using a while loop, print out the countdown output above.
*/
// your code goes here
var sec = 60;
while (sec >= 0) {
if (sec === 50 ) {
console.log("Orbiter transfers from ground to internal power");
sec = sec - 1;
continue;
} else if (sec === 31) {
console.log("Ground launch sequencer is go for auto sequence start")
sec = sec - 1;
continue;
} else if (sec === 16) {
console.log("Activate launch pad sound suppression system")
sec = sec - 1;
continue;
} else if (sec === 10) {
console.log("Activate main engine hydrogen burnoff system")
sec = sec - 1;
continue;
} else if (sec === 6) {
console.log("Main engine start")
sec = sec - 1;
continue;
} else if (sec === 0) {
console.log("Solid rocket booster ignition and liftoff!")
sec = sec - 1;
continue;
} else {
console.log("T-" + sec + " seconds");
sec = sec - 1;
continue;
}
}
/* For Loop
for (var i = 0; i < 6; i = i + 1) {
console.log('Printing out i = " + i);
}
x++ or ++x // same as x = x + 1
x-- or --x // same as x = x - 1
x += 3 // same as x = x + 3
x -= 6 // same as x = x - 6
x *= 2 // same as x = x * 2
x /= 5 // same as x = x / 5
*/
/* FACTORIAL of a number */
var solution = 1;
for (var num = 1; num <= 12; num += 1) {
solution = solution*num;
}
console.log(solution);
/* CINEMA ROWS AND SEAT */
for (var row = 0; row < 26; row += 1) {
for (var seat = 0; seat < 100; seat += 1)
console.log(row + "-" + seat)
}