Using an array as a parameter - Credit Card Checker

Hello,
I am having trouble using an array as a parameter in a function in the credit card checker. Shouldn’t the below code output:
Odd, even, odd, even, etc…

Am I messing up calling the function or somewhere else?

Link to Credit Card Checker:
https://www.codecademy.com/practice/projects/credit-card-checker

Code:

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

const validateCred = (card) => {

//looping through card number using card paramater which is an array

for(let i = card.length - 1; i < 0; i–) {

//if the card index digit is even

if(i % 2 === 0) {

  return 'Index number of array even';

} else {

  return 'Index number of array odd';

};

};

};

console.log(validateCred(valid1));

I think the for-loop is causing the main issue. Right now you are setting i initially to card.length - 1. But your condition for the loop to run is that i is less than zero (i < 0). So the loop will not run. A more common approach would probably be to start with i = 0, with a condition that i < card.length and increasing i with i++. Return will also exit the function as well as stopping the loop.

Also, you can paste code in using the </> sign so it is easier to read.

1 Like

Thanks.

I want it to cycle through the credit card number backwards, checking if each index number is even or odd.

So starting at the index of 15 (card.length - 1) and stopping after the index of 0 (i < 0).

I am getting an output of undefined when i run my code.

The for loop has the condition for when the loop keeps going and not when it stops. Therefore you can change i < 0 to i > 0 (change direction on the arrow) and that should fix your issue. Also make sure to use:

i--

to decrease the value of i. You probably already are using this but it is hard to see from code if it is not typed using the </> mode of the forum editor.

1 Like

We see a lot of learners reversing the array so as to get the correct index on the doubling. This is added headroom that we don’t need to add to the process.

We know that the last digit is not to be touched. That tells us something. The target element is the second last one. Where we start is all we need to know, beyond that.

0 1 0 1 0 1 0 1 0 1 0      //  odd length

1 0 1 0 1 0 1 0 1 0 1 0    //  even length

The 1’s are the doubles.

in the if change the return to console.log and it should work, if I’m not mistaken it’s because return doesn’t allow you to re-enter the loop. I hope I’ve helped

You mean it doesnt matter if the loop starts from index 0 or 15 because it all gets added anyway?

That is a bit of a blanket statement that one would not want to confirm, but if we didn’t mess with doubled values and just left them alone then added all the digits we would get the same sum as we do with the fix scenario. It’s just more difficult to add digits in programs because numbers cannot be split. We need to sequence them to break them apart. At that point we can sum them up as single digits once they are back as numbers.

Of course I’m probably way off the topic so let me veer back on. The first rule is that the last element never gets changed, and the second rule is that the second last one always gets changed. Now trace backwards every other element. We are either going to end on the second or the first element. That is our determining factor in terms of where to begin doubling. After that it’s just every other one. Budda-bing, budda-boom.


This function uses the digit sum approach for no other reason than to be able to do it.

const luhn = t => {
  const g = (a, b) => +a + +b
  const f = x => [...(x.toString())].reduce(g)
  const s = t.slice()
  for (let n = s.length % 2; n < s.length; n += 2) {
    s[n] = f(s[n] * 2)
  }		
  return s.reduce(g) % 10 == 0
}

What we see from this approach when we examine more closely is that the reduction of the whole list minus the last is equal to the last. That may be escaping some or many learners. Just tossing that in.