-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter2.js
More file actions
82 lines (71 loc) · 1.81 KB
/
chapter2.js
File metadata and controls
82 lines (71 loc) · 1.81 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
console.log('Chapter 2 Exercise 1')
let star = '';
while (star.length < 8) {
star += '*';
console.log(star);
}
// cleaner solution given by book
// for (let line = "#"; line.length < 8; line += "#")
// console.log(line);
console.log(' ')
console.log(' ')
console.log(' ')
console.log('Chapter 2 Exercise 2')
for (let i = 1; i < 101; i++) {
if (i % 5 === 0 && i % 3 === 0) {
console.log(i + ' FizzBuzz');
} else if (i % 3 === 0) {
console.log(i + ' Fizz');
} else if (i % 5 === 0) {
console.log(i + ' Buzz');
} else {
console.log(i)
}
}
// cleaner solution given by book
// for (let n = 1; n <= 100; n++) {
// let output = "";
// if (n % 3 == 0) output += "Fizz";
// if (n % 5 == 0) output += "Buzz";
// console.log(output || n);
// }
console.log(' ')
console.log(' ')
console.log(' ')
console.log('Chapter 2 Exercise 3')
function gridBySize(size = 8) {
let grid = '';
for (let row = 0; row < size; row++) {
let rowString = '';
if (row > 0)
rowString += '\n';
for (let column = 0; column < size; column++) {
if (row % 2 === 0) {
if (column % 2 === 0)
rowString += '#'
else
rowString += ' '
} else {
if (column % 2 === 0)
rowString += ' '
else
rowString += '#'
}
}
grid += rowString
}
console.log(grid);
}
gridBySize();
// cleaner solution given by book
// let board = "";
// for (let y = 0; y < size; y++) {
// for (let x = 0; x < size; x++) {
// if ((x + y) % 2 == 0) {
// board += " ";
// } else {
// board += "#";
// }
// }
// board += "\n";
// }