Bishop And Pawn
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"
andpawn = "c3"
, the output should bebishopAndPawn(bishop, pawn) = true
. -
For
bishop = "h1"
andpawn = "h3"
, the output should bebishopAndPawn(bishop, pawn) = false
.
Input/Output
-
[execution time limit] 4 seconds (js)
-
[input] string bishop
Coordinates of the white bishop in the chess notation.
Guaranteed constraints:
bishop.length = 2
,
'a' ≤ bishop[0] ≤ 'h'
,
1 ≤ bishop[1] ≤ 8
.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.
Guaranteed constraints:
pawn.length = 2
,
'a' ≤ pawn[0] ≤ 'h'
,
1 ≤ pawn[1] ≤ 8
. -
[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
11
12
function bishopAndPawn(bishop, pawn) {
var bishopCoords = bishop.split("");
var pawnCoords = pawn.split("");
bishopCoords[0] = 1 + bishopCoords[0].charCodeAt(0) - "a".charCodeAt(0);
bishopCoords[1] = Number(bishopCoords[1]);
pawnCoords[0] = 1 + pawnCoords[0].charCodeAt(0) - "a".charCodeAt(0);
pawnCoords[1] = Number(pawnCoords[1]);
return (
bishopCoords[0] - bishopCoords[1] === pawnCoords[0] - pawnCoords[1] ||
bishopCoords[0] + bishopCoords[1] === pawnCoords[0] + pawnCoords[1]
);
}