-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathCheckered Board.js
55 lines (47 loc) · 1.98 KB
/
Checkered Board.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
/*
Description:
Write a function which takes one parameter representing the dimensions of a checkered board. The board will always be square, so 5 means you will need a 5x5 board.
The dark squares will be represented by a unicode white square, while the light squares will be represented by a unicode black square (the opposite colours ensure the board doesn't look reversed on code wars' dark background). It should return a string of the board with a space in between each square and taking into account new lines.
An even number should return a board that begins with a dark square. An odd number should return a board that begins with a light square.
The input is expected to be a whole number that's at least two, and returns false otherwise (Nothing in Haskell).
Examples:
checkeredBoard(5)
returns the string
■ □ ■ □ ■
□ ■ □ ■ □
■ □ ■ □ ■
□ ■ □ ■ □
■ □ ■ □ ■
There should be no trailing white space at the end of each line, or new line characters at the end of the string.
Note
Do not use HTML entities for the squares (e.g. □ for white square) as the code doesn't consider it a valid square. A good way to check is if your solution prints a correct checker board on your local terminal.
Ruby note: CodeWars has encoding issues with rendered unicode in Ruby. You'll need to use unicode source code (e.g. "\u25A0") instead of rendered unicode (e.g "■").
*/
function checkeredBoard(dimension) {
if (dimension!==parseInt(dimension)||dimension<2) return false
let light=true
let arr=[]
if (dimension%2===0){light=false}
for (let i=0;i<dimension;i++){
let temp=''
for (let j=0;j<dimension;j++){
if (!light){
if (j%2==0){
temp+='□ '
}else {
temp+='■ '
}
}else {
if (j%2!=0){
temp+='□ '
}else {
temp+='■ '
}
}
}
temp=temp.trim()
arr.push(temp)
light=!light
}
return arr.join`\n`
}