Credit Card Checker Project

Hi!
I am attempting the credit card checker project, but I somewhat have no idea what I am doing. I tried to make some code that was close to the steps the project laid out, but it’s not pushing anything into the array?
If anyone could help me to understand why my code isn’t pushing the numbers onto the array I would be eternally grateful!

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]; function validateCred(array){ for(let i = array.length; i >= 0; i--){ // an array to store the updated numbers let validationNumbers = []; if (array[i] === array[15]){ validationNumbers.push(array[i]); console.log(validationNumbers); }else if (array[i] % 2 != 0){ let updatedNumbers = array[i] * 2; console.log(validationNumbers); if(updatedNumbers > 9){ updatedNumbers = updatedNumbers - 9; } validationNumbers.push(array[i]); } return validationNumbers; } const reducer = (accumulator, curr) => accumulator + curr; let sum = validationNumbers.reduce(reducer); if (sum % 10 === 0){ console.log('true'); }else{console.log('false')} } validateCred(valid1);

Hi,
there are several problems, the first and most important is this:

for(let i = array.length; i >= 0; i--){

Your loop will only run once as the exit condition i >= 0 is fulfilled from the start: If your array has 16 digits, you start at 16. 16 >= 0 is true.

Then you have this:

if (array[i] === array[15])

You want to omit every other digit in the array, right? Then you need to know, at which round of the loop you are, not if the digit itself is devidable by two without a rest value: array[i] in the first loop is ‘undefined’ as there is no element at place 16 in the array. array[i] in the second loop is ‘8’.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.