Credit Card Checker - returning false even with valid credit card numbers

Can anyone please tell me where in the code I’ve gone wrong?

const validateCred = array => {
  newArray =[]
  for (var i = array.length - 2; i >= 0; i -= 2) { 
  let doubleValue = array[i] * 2 
   if (doubleValue > 9) {
     doubleValue -= 9;
  }  newArray.push(doubleValue)

  for (var j = array.length -1; j >= 0; j -=2) {
    let combined = [].concat(newArray, array[j]);
     if (combined.reduce((prv, cur) => prv + cur) % 10 === 0) {
       return true
     } else {
       return false
     }
  } 
  }
  }


console.log(validateCred(valid1))


Hi,
what type is your parameter ‘valid1’? If it is typeof number, your for loop will not iterate, because ‘array.length’ is undefined. You can only get the length of a string or amount of elements in an array.

Your for loop will only iterate once (since the logic either leads to a true or false after the first iteration); is this the intended behaviour of the loop?

1 Like

Hi @codeneutrino. That for is for the sum of all the digits in the credit card number. So it’s okay if it only iterates once isn’t it? If I instead of return type console.log('true') and console.log('false') they print tens of times on the console, and the pattern is 13 times false and 1 time true. I think it’s because of the way the first loop is logging onto the console. It’s logging every iteration to the console. How do I stop this from happening?

Here’s the code and the output:

const validateCred = array => {
  newArray =[]
  for (var i = array.length - 2; i >= 0; i -= 2)
{ 
  let doubleValue = array[i] * 2 
     if (doubleValue > 9) {
     doubleValue -= 9;
  }  newArray.push(doubleValue)
    console.log(newArray)
}
}
    
    
     
console.log(validateCred(valid1))


Output:
[ 0 ]
[ 0, 3 ]
[ 0, 3, 0 ]
[ 0, 3, 0, 0 ]
[ 0, 3, 0, 0, 5 ]
[ 0, 3, 0, 0, 5, 3 ]
[ 0, 3, 0, 0, 5, 3, 6 ]
[ 0, 3, 0, 0, 5, 3, 6, 8 ]
undefined

Here the array it’s iterating through:

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];

Hi @mirja_t. This is the parameter valid1:

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];

Sorry, I haven’t got too much of an idea of this project; I don’t have PRO. Have you followed the correct Luhn algorithm steps? Is the output what you’d expect? What do you intend to do with the output?

Hi @codeneutrino, that’s okay. Yes, I’ve followed the steps correctly. I expected the output to be ‘true’ because I passed in a valid credit card number. Here are the steps for this section of code:

Create a function, validateCred() that has a parameter of an array. The purpose of validateCred() is to return true when an array contains digits of a valid credit card number and false when it is invalid. This function should NOT mutate the values of the original array.

To find out if a credit card number is valid or not, use the Luhn algorithm. Generally speaking, an algorithm is a series of steps that solve a problem — the Luhn algorithm is a series of mathematical calculations used to validate certain identification numbers, e.g. credit card numbers. The calculations in the Luhn algorithm can be broken down as the following steps:

  1. Starting from the farthest digit to the right, AKA the check digit, iterate to the left.
  2. As you iterate to the left, every other digit is doubled (the check digit is not doubled). If the number is greater than 9 after doubling, subtract 9 from its value.
  3. Sum up all the digits in the credit card number.
  4. If the sum modulo 10 is 0 (if the sum divided by 10 has a remainder of 0 ) then the number is valid, otherwise, it’s invalid.

So, firstly, you need to have every digit added up, and only every second digit added. You also need to start from the check digit:

arr = [1, 2, 3, 4, 5, 6]
          ^     ^     ^
          |     |     |
          |     |     Check digit
      Number to double

So the array after the Luhn algorithm should look like:

arr = [1, 4, 3, 8, 5, 6]
          ^     ^     ^
          |     |     |
These numbers doubled |
    You don't double the check digit

Then, you need to add up all of the digits in arr, and perform the modulo operation:

sumOfArr = 1 + 4 + 3 + 8 + 5 + 6 = 27
sumOfArr % 10 = 7
(27 / 10 = 20 remainder 7)

You need to implement all of these steps in your code.

Hi @codeneutrino. I believe I’ve done that? Here’s all of my code so far:


const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];


const validateCred = array => {
  newArray =[]
  for (var i = array.length - 2; i >= 0; i -= 2)
{ 
  let doubleValue = array[i] * 2 
     if (doubleValue > 9) {
     doubleValue -= 9;
  }  newArray.push(doubleValue)
  
    
 for (var j = array.length -1; j >= 0; j -=2) 

     
 {
    let combined = [].concat(newArray, array[j]);
     
      if (combined.reduce((prv, cur) => prv + cur) % 10 === 0) {
      console.log('true');
    } else {
      console.log('false');
     }
  } 
  }
  }

validateCred(valid1)


This loop seems to be nested; is that what you want? Also, if your intention is for this loop to only iterate once, is there any reason you need it at all?

Also, the original loop starts on the second last element, doubling the wrong number, I think.

Sorry for sounding harsh; you’ve almost got it and I don’t want to give too much away!

Hi @codeneutrino.

I think* I had to have a nested array because the instructions say to create the Luhn Algorithm in one function. Is it possile to have two loops within the same function? My intention was for the loop to iterate over every other digit in the array starting from the last one on the right and iterating backwards.

The original loop must start from the second to last element, as per the instructions.

Hello @vickyrai hope you are safe!

Idk if you were able to figure out how to solve this challenge so here I will pass my code which was really inspired in yours. If you have any question or comment let me know! :nerd_face:

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]; const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9]; const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6]; const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5]; const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6]; // Add your functions below: const validateCred = array => { let doubleArray = []; let normalArray = []; let finalArray = []; for (let i = array.length - 2; i >= 0; i -= 2) { let doubleItem = array[i] * 2; if (doubleItem > 9) { doubleItem -= 9; }; doubleArray.push(doubleItem); } for (let j = array.length - 1; j >=0; j -= 2) { let normalItem = array[j]; normalArray.push(normalItem); } finalArray = normalArray.concat(doubleArray) const reducer = (accumulator, currentValue) => accumulator + currentValue; if (finalArray.reduce(reducer) % 10 === 0) { return true; } else { return false; } } console.log(validateCred(valid1));

Your friend Manuel

Awesome! Well done! That was a long time ago!! I did figure it out in the end! I’m on Jamming in React now!

1 Like