Chess Board Cell Color

Description


Given two cells on the standard chess board, determine whether they have the same color or not.

Example

  • For cell1 = "A1" and cell2 = "C3", the output should be chessBoardCellColor(cell1, cell2) = true.

  • For cell1 = "A1" and cell2 = "H3", the output should be chessBoardCellColor(cell1, cell2) = false.

Input/Output

  • [execution time limit] 4 seconds (js)

  • [input] string cell1

  • [input] string cell2

  • [output] boolean

true if both cells have the same color, false otherwise.

[JavaScript (ES6)] Syntax Tips

1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
    console.log("This prints to the console when you Run Tests");
    return "Hello, " + name;
}

Solution


1
2
3
4
5
function chessBoardCellColor(cell1, cell2) {
    return cell1.split('').reduce((a,b)=> 
                                  Math.abs(a.charCodeAt(0) - 'A'.charCodeAt(0) + 1 - b)%2)  === cell2.split('').reduce((a,b)=> 
                                  Math.abs(a.charCodeAt(0) - 'A'.charCodeAt(0) + 1 - b)%2)
}