Node.js examples for Array:Array Value
Highest sum from array of credit cards
var cardNumbers = ['1234-2345-5432-2345', '5432-2345-2345-2345', '5432-2345-5432-2435', '5432-2345-5432-2341']; var findLargest = function(cardArray) { var revisedNumbers = []; var ccSums = []; var indexCounter = 0; var highNum = 0; cardNumbers.forEach(function(card) { var noDash = card.replace(/-/g, ""); revisedNumbers.push(noDash);//from w ww. j a v a 2 s.c o m }); revisedNumbers.forEach(function(base) { var integerArray = base.split('').map(Number); var eachSum = integerArray.reduce(function(a,b) { return a + b; }, 0); ccSums.push(eachSum); }); //loop through the sums array //use highNum as a constant to check for greater value //if greater value, reset the highNum t equal that value //and set indexCounter to equal that index number (for tracking) for (var i=0; i<ccSums.length; i++){ if(ccSums[i] > highNum){ highNum = ccSums[i]; indexCounter = i; } } //now check for the last index of that highest number var last = ccSums.lastIndexOf(highNum); return cardArray[last]; } console.log(findLargest(cardNumbers));