Created
May 21, 2019 03:11
-
-
Save jcunanan05/2d9adfef72d46c2977bbaf03d9b4539d to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* war.js | |
* Algorithm complexity might be O(n + 4). 4 Lookups + 1 Loop | |
*/ | |
const CARD_ORDER = 'AKQJT98765432'; | |
function war(handA, handB) { | |
let totalScore = 0; | |
for (let i = handA.length - 1; i >= 0; i--) { | |
console.log( | |
`handA: el: ${handA.charAt(i)} order: ${cardOrderOf( | |
handA.charAt(i) | |
)} VS handB: el: ${handB.charAt(i)} order: ${cardOrderOf( | |
handB.charAt(i) | |
)} win: ${cardOrderOf(handA.charAt(i)) < | |
cardOrderOf(handB.charAt(i))} totalScore: ${totalScore}` | |
); | |
if (cardOrderOf(handA.charAt(i)) < cardOrderOf(handB.charAt(i))) { | |
totalScore += 1; | |
} | |
} | |
return totalScore; | |
} | |
function cardOrderOf(card) { | |
return CARD_ORDER.indexOf(card); | |
} | |
console.log('AT3K2 VS QJ795:', war('AT3K2', 'QJ795')); // 2 | |
console.log('72Q8A VS 2KQ63:', war('72Q8A', '2KQ63')); // 3 correction in the docx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment