I am working on a coding challenge and believe I am close to a solution, but am currently stuck and would appreciate some insight. I am trying to build a function that can take a set of numbers (credit card numbers including dashes), compare the sum of each number, and return the highest credit card number. The solution must utilize Javascript. The function is currently returning "undefined". Thanks in advance for any help!
function highestCredit(creditArray){
var highSum = 0;
var highIndex = 0;
var sum= 0;
//loop through each value in the array
for (i = 0; i < creditArray.length; i++) {
//loop that disregards dashes and adds up the sum of a single credit card number
for (a = 0; a < creditArray[i.length]; a++) {
if(isNaN(creditArray[i].charAt(a)) === false){
sum += parseInt(creditArray[i].charAt(a));
}
}
//compare current sum to highest sum, update the index if there is a new high number
if (sum >= highSum) {
highSum = sum;
sum = 0;
highIndex = i;
}
}
return creditArray[i];
}
console.log(highestCredit(['4916‐2600‐1804‐0530', '4779‐252888‐3972', '4252‐278893‐7978' ,'4556‐ 4242‐9283‐2260']));
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…