Skip to content

Instantly share code, notes, and snippets.

@jcunanan05
Created May 21, 2019 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcunanan05/2d9adfef72d46c2977bbaf03d9b4539d to your computer and use it in GitHub Desktop.
Save jcunanan05/2d9adfef72d46c2977bbaf03d9b4539d to your computer and use it in GitHub Desktop.
/**
* 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