Why does my loop not .push into a array created before?

I am currently working on the credit card checker, trying to implement the Free Formatter’s Luhn algorithm implementation. Unless I think i am on the right way with “pseudo-coding”, my code does not work. I tried to debug it with console.log. So far I found out, that the loop I created does not .push the result of the loop to the array created, but I can not figure out why. I would be glad about any hint! Thanks a lot!

The loop I am talking about is in line 42. Until there the code seems to work.

PS: This is my first topic I creat in a forum. I hope it fits the criteria for a legit topic.


I think you’re talking about this line:


It does push the digit to the array, actually. It logs [8] when you run your function validateCred with valid1 and then log newNumber.
There are two problems with that:

  1. It is not inside a loop, therefore only one digit is pushed to the array.
  2. The .pop() method mutates the original array, therefore valid1 looks different after you ran the function.

If you need help with your code, it will probably happen faster if you post the topic in the Get help > Javascript category.

Thanks for the fast reply!
Well, this was intended, as the Free formatter’s luhn removes the last digit and reverses the loop
==> Credit Card Number Generator & Validator - FreeFormatter.com.

But I agree with you, maybe `newNumber.push(array(array.length - 1) would be a better solution = removing last digit without changing the array. Correct.

The problem I don’t get is on line 42 newNumber.push(reversedArray[i] - 9). I would expect the loop on line 39 for(let i = 0; i >= reversedArray.length; i++) to execute line 42 (and following) and push further elements to the array newNumber (what does not happen as you stated correctly).

I appreciate your help. If this conversation leads to far I will happily move to the Get help > Javascript category.

The loop never starts: The condition i >= reversedArray.length (if i is larger or equal to reversedArray.length) is false from the start.