← Back to challenges

Knights Jump

JavaScriptHardalgorithmsarrayslogicgames

Instructions

Knights can jump across the board.

Knights in Chess

Create a function that takes a square where a knight jumps from as a string and returns all the possible squares the knight can land on as a string. Ignore capturing and further Chess rules. Be aware of the sides of the board. Knights don't go over the edge, obviously.

Examples

knightsJump("F4") ➞ "E2,G2,D3,H3,D5,H5,E6,G6"

knightsJump("A1") ➞ "C2,B3"

knightsJump("E2") ➞ "C1,G1,C3,G3,D4,F4"

Notes

  • The input is always a valid square on the board.
  • The order of the return string is 1 ➞ 8, A ➞ H.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.