Created
December 13, 2016 22:12
-
-
Save jamesxv7/85ed182197c6af22d83d9053f1a7ccb2 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
//Kata: https://www.codewars.com/kata/vasya-clerk/train/javascript | |
function tickets(peopleInLine){ | |
var ticketPrice = 25 | |
var bills = [0, 0, 0] | |
// Just count the bills... forget the register value | |
for (var i = 0; i < peopleInLine.length; i++) { | |
if (peopleInLine[i] == ticketPrice) { | |
bills[0] += 1 // Clients with $25 are the easy ones | |
} else if (peopleInLine[i] > ticketPrice) { | |
if (peopleInLine[i] == 50) { // Clients with $50 are easy too | |
if (bills[0] == 0) | |
return "NO" | |
else { | |
bills[0] -= 1 | |
bills[1] += 1 | |
} | |
} else if (peopleInLine[i] == 100) { // The tricky part, the change for $100 bills | |
if ((bills[0] <= 2 && bills[1] == 0)) | |
return "NO" | |
else { | |
if (bills[1] == 0) // No more $50s | |
bills[0] -= 3 | |
else { | |
bills[0] -= 1 | |
bills[1] -= 1 | |
} | |
bills[2] += 1 // Cashing | |
} | |
} | |
} | |
} | |
return "YES" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment