Chessboard: findColor

mail@pastecode.io avatar
unknown
javascript
a year ago
670 B
4
Indexable
Never
/**
 * 
 * @param {boolean} startingColor true: black false: white
 * @param {number} x positive number
 * @param {number} y positive number
 * @returns true if the coordinates of (x,y) cell equal to first cell (1,1) color
 */
const findColor = (startingColor, x, y) => {
    cell1 = 1 + 1;
    cell2 = x + y;

    if (!Number.isInteger(x) || !Number.isInteger(y))
     return 'please enter a valid values.'

    return startingColor ? cell1 % 2 === cell2 % 2 : !(cell1 % 2 === cell2 % 2) 
}

console.log(findColor(false, 1, 2)) // true
console.log(findColor(true, 1, 2)) // false

console.log(findColor(false, 2 , 2)) // false
console.log(findColor(true, 2, 2)) // true