Last active
November 17, 2018 00:34
-
-
Save IegorT/9dabc4168714a07a78f712a5cae26105 to your computer and use it in GitHub Desktop.
UniLecs - Задача 140
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isHorseMove = str => { | |
if(!/^[A-H][1-8]-[A-H][1-8]$/.test(str)) throw new Error('wrong incoming data'); | |
// (c1, r1) - start coodinate, (c2, r2) - coordinate after move a figure | |
const [c1, r1, _, c2, r2] = str.split('').map(ch => ch.charCodeAt(0)); | |
return ( | |
(Math.abs(c1 - c2) === 1 && Math.abs(r1 - r2) === 2) | |
|| | |
(Math.abs(c1 - c2) === 2 && Math.abs(r1 - r2) === 1) | |
) | |
}; | |
console.assert(isHorseMove('B1-C3') === true); | |
console.assert(isHorseMove('D4-C2') === true); | |
console.assert(isHorseMove('D4-B3') === true); | |
console.assert(isHorseMove('D4-B5') === true); | |
console.assert(isHorseMove('H1-G3') === true); | |
console.assert(isHorseMove('H1-F2') === true); | |
console.assert(isHorseMove('E2-E4') === false); | |
console.assert(isHorseMove('C2-C8') === false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment