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");
        }
    }
}
};
2

There are 2 answers

0
Himanshu Sharma On

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

 arrComparison(array1, array2);

Or if you want it to invoke itself automatically,

var arrComparison = function({

})();
0
jaredsk On

There are a few errors here. In this for loop, you have a return statement, but this isn't a function:

for (var b = 0; b < tempArray.length; b++) {
     var csum = sumDigits(tempArray[b]);
     if (csum >= currentHighest) {
        currentHighest = csum;
        largest = inputArray[b];
        return largest;
     }
 }

You also define tempArray in your first function, but don't return anything from the function. When you try to access tempArray from the for loop, it isn't defined and throws an error.

Also, if I understand your problem correctly, your solution is more complex than it needs to be. For example, if you want to sum the numbers represented by your strings, try something like this:

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

function sumDigits(digString) {
  var sums = [];
  for (num in cc) {
    var chunks = digString[num].split('-');
    var sum = 0;
    for (item in chunks) {
      sum += (parseInt(chunks[item]));
    }
    sums.push(sum);
  }
  return sums
}

var sums = sumDigits(cc);