-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckerboard.js
32 lines (31 loc) · 1.04 KB
/
checkerboard.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
const createCheckerboard = (num) => {
// Create empty string to hold a row
let row = ""
// Create empty string to hold our checkerboard
let checkerboard = ""
// Create row: Iterate up until the num argument
for(let i = 0; i < num; i++){
// If iterator is even add a "#" to the row string
if(i % 2 === 0){
row += "#"
} else {
// Else add " " to the row string
row += " "
}
}
// Create checkerboard: iterate up until the num argument
for(let i = 0; i < num; i++){
// if iterator is even, add row string to checkerboard string, and add new line character
if(i % 2 === 0){
checkerboard += row
checkerboard += "\n"
} else {
// else: flip the string and add it to the checkboard, and add new line character
checkerboard += row.split('').reverse().join('')
checkerboard += '\n'
}
}
// Return checkerboard string
return checkerboard
}
console.log(createCheckerboard(8))