-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharp-grid.js
58 lines (43 loc) · 1.05 KB
/
sharp-grid.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
/*
Write a progran that create a string that represents an 8x8 grid, using newline
characters to separate lines. At each position of the grid ther is either a space
or a "#" character. The characters should form a chessboard.
Passing this string to console.log should show something like this:
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
When you have a program that generates this pattern, define a binding size = 8
and change de program so that it works for any size, outputting a grid of the
given width and heigh.
*/
let size = 8;
let string = ' ';
for (let num = 0; num < size; num++) {
if (num % 2 == 0) string += ' ';
else string += '#';
}
for (let row = 0; row < size; row++) {
if (row % 2 == 0) console.log(string);
else console.log(string.replace(' ', ''));
}
/*
solution:
let size = 8;
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";
}
console.log(board);
*/