-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCh1.js
63 lines (54 loc) · 1.28 KB
/
Ch1.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
// do while loop
do {
var name = prompt("Who do you think you are?");
}
while (!name);
console.log(name)
// for loop: even numbers up to 12
for (var number = 0; number <= 12; number += 2);
console.log(number)
// for loop: 2 to the 10th
var result = 1;
for (var counter = 0; counter < 10; counter++)
result *=2;
console.log(result)
// example of break statement
for (var current = 20; ; current++){
if (current % 7 == 0)
break;
}
console.log(current);
// Exercise 1a: looping a triangle (function)
var loopingThroughTriangle = function(){
var hash = "";
for (var counter = 0; counter <8; counter++){
console.log(hash);
hash += "#";
}
};
loopingThroughTriangle();
// Exercise 1b: looping a triangle (just loop)
for (var hashLine = "#"; hashLine.length <8; hashLine += "#")
console.log(hashLine);
// Exercise 2a: FizzBuzz
for (var i = 1; i <= 100; i++)
if(i % 3 == 0 && i % 5 ==0){
console.log("FizzBuzz");
}
else if (i % 3 == 0 && i % 5 !=0){
console.log("Fizz");
}
else if(i % 5 == 0 && i % 3 !=0){
console.log("Buzz");
}
else
console.log(i);
// Exercise 2b: FizzBuzz My code works, but here's the author's solution
for (var n = 1; n <= 100; n++) {
var output = "";
if (n % 3 == 0)
output += "Fizz";
if (n % 5 == 0)
output += "Buzz";
console.log(output || n);
}