 # Credit Card Checker sum acting weird

Hello! I am on step 3 of the Credit Card Checker project wherein we seek to implement a Luhn algorithm to check the validity of a credit card number:https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-challenge-project-credit-card-checker/projects/credit-card-checker

My code seems to be working until I enter the section where I sum up the array using a for loop. I am attempting to sum the runningTotal array, which when logged just before the sum loop outputs an array of 16 numbers, I keep getting just a one digit number as my output for sum, and thus it is reading every card as invalid even when it is incorrect.

let validateCred = (array) => { let runningTotal = []; //adds last element to array runningTotal.push(array[array.length - 1]); //return runningTotal; //creates new array with which to perform calculations let newArray = []; newArray = array.reverse(); //removes first element of array newArray.shift(); //return newArray; //iterate through even numbers for (let i = 0; i < newArray.length; i+= 2){ runningTotal.push(newArray[i]); } //iterate through odd numbers for (let i = 1; i < newArray.length; i+= 2){ //multiply each number by 2 let newNumber = (newArray[i] * 2); //convert 2 digit numbers into 1 digit if (newNumber > 9){ newNumber = (newNumber - 9); } else { newNumber; } runningTotal.push(newNumber); } //total all numbers in array let sum = 0; for (let i = 0; i < runningTotal.length; i++){ sum += runningTotal[i]; return sum; } //evaluate whether number ends in zero if (sum % 10 === 0){ return 'Credit card is valid.'; } else { return 'Credit card is invalid.'; } }

I can’t figure out what is wrong with my sum for loop. I have checked over it so many times and even ran it separately in its own file, where it functions properly.

Any help or insight would be appreciated.

Hi!

You have a return statement in a for-loop. Consider the implications of that.

But more importantly you should review what it means to return something in a function. How many times can/should a function return something?

1 Like