What I'm trying to accomplish here is to find the sum of all the numbers, organize them by ascending order according to the sum, and determine which string is the largest.
var cc = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'];
This function finds the highest number by converting the string into summable numbers.
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, ''));
}
This one sums them.
function sumDigits(strA) {
var highest=0;
var sum = 0;
var largest=0;
for (var i = 0; i < strA.length; i++)
sum += parseInt(strA.charAt(i), 10);
return sum;
}
for (var b = 0; b < tempArray.length; b++) {
var csum = sumDigits(tempArray[b]);
if (csum >= currentHighest) {
currentHighest = csum;
largest = inputArray[b];
}
}
cc.forEach(function (b) {
total = b.match(/\d/g).reduce(function (r, b) {
return r, ++b;
});
});
This one takes both arrays, matches and compares them.
var arr0 = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'];
var arr1 = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'];
var arrComparison = function(arr0,arr1) {
for(var t = 0; t <= arr0.length -1; t++) {
for(var j =0; j <= arr1.length-1; j++) {
if(arr0[t] === arr1[j]) {
console.log(arr0[t] + " and " + arr1[j] + " are the same");
}
}
}
};
Kindly post the error that you are getting. I don't think you are calling the comparison function. You are just defining it and storing it in a variable arrComparison. You may call the function by
Or if you want it to invoke itself automatically,