Credit Card Checker Javascript

Hello,

I am a newbie to programming and please be patient in helping me. So I am trying to code this challenging project. I don’t want to copy and paste on what others did but wanted to try myself. I couldn’t figure out what I am doing wrong below. Per the instructions, I tried but not going anywhere. Could someone help me with this?

// Add your functions below:

const validateCred = card => {
  let checkCard = card.splice(-1);
  let newArr = card.reverse();
  console.log(newArr);
  for(let i =0; i<newArr.length; i++){
    if(i % 2 === 0){
      newArr[i] = newArr[i] * 2;
      if (newArr[i] > 10){
        newArr[i] = newArr[i] - 9;
        return newArr[i];
      }
    }
  }
}

console.log(validateCred(valid3));

The output is

[ 3, 2, 5, 8, 9, 9, 1, 0, 2, 1, 6, 1, 7, 3 ]
9

Could someone tell me what is not looping the array?

Thanks

> Blockquote

If you’re asking simply why you’re only getting one number from the for loop, you have to trace the path of your code’s logic. console.log() is your friend here, use it to see what is happening at different points in your code. You are looping through the array but it’s your if statements and the return statement that cause your function to stop at index 4 of the array and return a 9. Also I would suggest looking for a different way to reverse the card numbers. Splice() and reverse() are both mutator methods.

2 Likes

thank you. could you suggest some methods on using splice and reverse?

This is where documentation is our friend. MDN slice() is an accessor method that is very useful for making a copy of an array.

thanks i will look into it. thanks for helping me.