Skip to content

Instantly share code, notes, and snippets.

@erottman
Created December 20, 2016 16:29
Show Gist options
  • Save erottman/c4f9478041147700e630fbb7ba130ef9 to your computer and use it in GitHub Desktop.
Save erottman/c4f9478041147700e630fbb7ba130ef9 to your computer and use it in GitHub Desktop.
var creditSum = function(creditNumbers) {
var max = 0; // This is the sum of the largest credit card
var largestSum; // This represents the string value of the largest credit card.
for(var i = 0; i < creditNumbers.length; i++) {
var total = 0;
var numberString = creditNumbers[i];
for(var j = 0; j < numberString.length; j++) {
var singleNumber = parseInt(numberString.charAt(j));
if(singleNumber) {
total += singleNumber;
}
}
if(total >= max) {
max = total;
largestSum = creditNumbers[i];
}
}
return largestSum;
};
var creditCards = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'];
creditSum(creditCards);
console.log(creditSum(creditCards));
'4252-278893-7978'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment