Bishop Degree

Description


Given the positions of a white bishop and a black pawn on the standard chess board, determine whether the bishop can capture the pawn in one move.

The bishop has no restrictions in distance for each move, but is limited to diagonal movement. Check out the example below to see how it can move:

Example

  • For bishop = "a1" and pawn = "c3", the output should be bishopAndPawn(bishop, pawn) = true.

  • For bishop = "h1" and pawn = "h3", the output should be bishopAndPawn(bishop, pawn) = false.

Input/Output

  • [execution time limit] 4 seconds (js)

  • [input] string bishop

Coordinates of the white bishop in the chess notation.

Note: Chess Notation - Each square of the chessboard is identified by a unique coordinate pair—a letter and a number. The vertical columns of squares from white’s left to the right are labeled 'a' through 'h'. The horizontal rows of squares are numbered 1 to 8 starting from white’s side of the board. Thus each square has a unique identification as a string consisting of two characters: the first is the column label, and the second in the row number.

  • [input] string pawn

Coordinates of the black pawn in the same notation.

  • [output] boolean

true if the bishop can capture the pawn, 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
6
7
8
9
10
function bishopAndPawn(bishop, pawn) {
    pawn = pawn.split('');
    pawn[1] = Number(pawn[1])
    pawn[0] = pawn[0].charCodeAt(0)-'a'.charCodeAt(0)+1;
    bishop = bishop.split('');
    bishop[1] = Number(bishop[1])
    bishop[0] = bishop[0].charCodeAt(0)-'a'.charCodeAt(0)+1;
    return Math.abs(pawn[0]-pawn[1]) === Math.abs(bishop[0]-bishop[1]) ||
         Math.abs(pawn[0]+pawn[1]) === Math.abs(bishop[0]+bishop[1])
}