Skip to content

Instantly share code, notes, and snippets.

@gabrielfurini
Created April 5, 2016 03:27
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 gabrielfurini/c211e8dc9a5ea76a2ecf568e46eb0a4c to your computer and use it in GitHub Desktop.
Save gabrielfurini/c211e8dc9a5ea76a2ecf568e46eb0a4c to your computer and use it in GitHub Desktop.
Javascript CASH MACHINE
//*** CASH MACHINE ***//
//THE PROBLEM
//Develop a solution that simulate the delivery of notes when a client does a withdraw in a cash machine.
//THE BASIC REQUIREMENTS ARE THE FOLLOW:
// Always deliver the lowest number of possible notes;
// It's possible to get the amount requested with available notes;
// The client balance is infinite;
// Amount of notes is infinite;
// Available notes R$ 100,00; R$ 50,00; R$ 20,00 e R$ 10,00
(function(){
'use strict';
function iWantToGet(ammountRequired){
var availableNotes = [100, 50, 20, 10],
result = [];
if(ammountRequired && ammountRequired > 0) {
while(ammountRequired > 0) {
for (var i = 0; i < availableNotes.length; i++) {
var note = availableNotes[i];
if(ammountRequired - note >= 0) {
ammountRequired -= note;
result.push(note);
break;
} else if(i === availableNotes.length - 1 && ammountRequired > 0) {
throw 'NoteUnavailableException';
}
}
}
} else if(ammountRequired <= 0) {
throw 'InvalidArgumentException';
}
return result;
}
console.log(iWantToGet(160));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment