Credit Card Checker Exercise - Help with never ending loop

I have a question regarding the credit card checker exercise (https://www.codecademy.com/practice/projects/credit-card-checker)

I was able to write the function to validate a credit card using the Luhn algorithm. However, when I try and write the function for identifying/storing invalid cards, I run into an never ending loop and I’d like to understand why.

Initially, I wrote the following function to test out whether I would be able to get back the card numbers:

const findInvalidCards = nestedCardArray => {
for (i = 0; i < nestedCardArray.length; i++){
console.log(nestedCardArray[i]);
}
}

findInvalidCards(batch)

This worked as expected, returning 15 separate sets of the card numbers…great! My thinking was that I could then take it one step further and be able to get back either a true or false statement by calling the validateCred function (as written below):

const findInvalidCards = nestedCardArray => {
for (i = 0; i < nestedCardArray.length; i++){
console.log(validateCred(nestedCardArray[i]));
}
}

findInvalidCards(batch)

However, when I call findInvalidCards, I run into an unending loop of “true” and “false”. I would have expected 15 ‘true’ or ‘false’ strings to be returned. Why is the unending loop occurring and how would I prevent it?

Thank you for any assistance.

Would you be able to post your code for your validateCred function too? what you’ve done there looks okay to me so I’m thinking it’s something to do with that function.

Sure. It’s as follows:

const validateCred = cardArray => {
for (i = cardArray.length - 1; i >= 0; i–){
if ((i - 1) % 2 !== 0) {
cardArray[i] *= 2;
if (cardArray[i] > 9) {
cardArray[i] -= 9;
}
}
}
const checkSum = cardArray.reduce(
(accumulator, currentValue) => accumulator + currentValue
);
return checkSum % 10 === 0;
}

Hello, @twoforflinching, and welcome to the forums.

Please review How do I format code in my posts?
That topic will show you how to post code in the forums that retains its original formatting, so it is more readable, and others who want to assist you will be able to copy/paste it into their own environment to test.

Happy coding!

I believe what is causing the error is your shared use of “i” as the loop variable in both loops. The order of operations is;

  1. Set i = 0
  2. Select ith element of nestedCardArray
  3. Input this to validateCred
  4. Set i = cardArray.length - 1
  5. Perform operation and increment i down to i = 0
  6. Increment i to i = 1

This is find on the first loop, but then the issue arises, because what happens next is

  1. i = 1 from loop
  2. Select ith element of nestedCardArray
  3. Input this to validateCred
  4. Set i = cardArray.length - 1
  5. Perform operation and increment i down to i = 0
  6. Increment i to i = 1

And as such i never makes it past 1, thus never ending the outer loop. Whenever you are writing for loops to be used in the same program always be careful of using the same incremental element more than once, as this can cause the infinite loop. Changing one of them to j and fixing their references throughout should fix the issue.

1 Like

@adamgaffney137183916 is spot on as to why you have an infinite loop. Personally, I would continue to use i as the iterator for both loops, but using the keyword let. When you declare a variable without any keyword in JavaScript, you give it global scope, and you’ve seen the result.

Aside, your validateCred function fails on valid3 and mystery3.

1 Like

I use R in my job and started with C++ so I’m still in the way of different iterators, but that makes perfect sense too!

1 Like

Thank you; I’ll be mindful of this in the future.

1 Like

Thank you for your answer. I made an incorrect assumption that i was scoped to the particular function it was in. I’ll review the section on scoping for a refresher. That’s an easy adjustment.

You are also mutating the original card numbers. If you examine valid1, for example, before and after your implementation of the Luhn algorithm you’ll see:

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

Thanks for the feedback on defining the variable. I’m going to play around with your suggestion. To me, i would prefer that i = iterator in each loop.

Also, I appreciate you noticing the bug. I checked valid1 and valid2, and the first mystery card number, but stopped there. A new puzzle for me to solve.

1 Like