Skip to content

Instantly share code, notes, and snippets.

@imcodingideas
Last active June 25, 2016 03:04
Show Gist options
  • Save imcodingideas/4987acdec1ed447a49bd54694371876f to your computer and use it in GitHub Desktop.
Save imcodingideas/4987acdec1ed447a49bd54694371876f to your computer and use it in GitHub Desktop.
credit card sum
/**
* Created by joseph on 6/6/16.
*/
function printNumbers(arrayOfCards) {
var reducedCards = [];
// iterate over array of cards that was passed to function
arrayOfCards.forEach(function(card){
card = card.replace(/-/g, '')
.split('')
.reduce(function(prev, curr){
return Number(prev) + Number(curr);
});
reducedCards.push(card);
});
return arrayOfCards[reducedCards.lastIndexOf(Math.max.apply(Math, reducedCards))];
}
console.log(printNumbers(['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260']));
console.log(printNumbers(['22-22', '99-90', '99-88', '79-99', '12-34']));
console.log(printNumbers(['22-22', '99-90', '79-99', '99-88', '12-34']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment