Alchemistioc - 1 year ago 76
Javascript Question

# What sort of command line is needed to find the sum of all the credit card numbers and have them organized in ascending order?

``````var cc = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'];
``````

function for summing each string

``````function highest(inputArray) {
var currentHighest = 0;
var largest = 0;
var tempArray = [];
for (var a = 0; a < inputArray.length; a++) {
tempArray.push(inputArray[a].replace(/\D/g, ''));
}
function sumDigits(strA) {
var sum = 0;
for (var i = 0; i < strA.length; i++) {
sum += parseInt(strA.charAt(i), 10);
}
return sum;
}
``````

organizing the strings in array

``````  for (var a = 0; a < tempArray.length; a++) {
var csum = sumDigits(tempArray[a]);
if (csum >= currentHighest) {
currentHighest = csum;
largest = inputArray[a];
}
}
return largest;
}
console.log(highest(cc));
``````

Here at the end is where my knowledge ends. Not sure what to use to call back the data so it is organized by order and sum.

You could use a hash table for the total and sort with the total of all digits.

``````var cc = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'],
total = {};

cc.forEach(function (a) {
total[a] = a.match(/\d/g).reduce(function (r, b) {
return r + +b;
}, 0);
});

cc.sort(function (a, b) {