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

Thank you for your reply!

Ah, I only added in the return statement to try and check for the bug (I should have edited that out before posting it.)
But I see what you are saying! Thank you! Originally my problem was that every array I input read as invalid even if it shouldn’t. Do you have any idea why that would be?

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]; } //evaluate whether number ends in zero if (sum % 10 === 0){ return 'Credit card is valid.'; } else { return 'Credit card is invalid.'; } }

My first guess is your sums aren’t adding up to a multiple of 10 (which I’m guessing is the condition you’re setting for validity). You just have to create a neat debugging pipeline to figure out why that is.

Console logs with descriptive template literals are your friend here (Template literals (Template strings) - JavaScript | MDN)