Card Checker Problems on Part 2: Electric Boogaloo

Hello Gang,

I’m on Part 4 of Card Checker:

My Validator works fine. When I attempt to work it into a batch tester, the code stops working after iterating over index 0. I’m not sure if this is because there’s a return in the validatecredit function or not, but I’ve gone through several forum posts, all the code is similarly structured to mine, at least if its different I’m missing it, and I’m not what I’m doing wrong.

Blockquote function validateCred(array){
let checkSum=0;
let reverseArray=array.reverse();
for ( i= 0; i < reverseArray.length; i++)
{if (i % 2 === 1 && 2reverseArray[i] >= 10)
{checkSum += (2
else if
(i % 2 === 1 && 2reverseArray[i] < 10)
{checkSum += 2
else if (i % 2 === 0)
{checkSum += reverseArray[i];}}
if (checkSum % 10 ===0)
{return true;}
{return false;}}

Blockquote let invalidCards=

function validateBatch(arrayList) {
for (i=0; i< arrayList.length; i++){
return invalidCards;
console.log(invalidCards); > Blockquote

If others are to inspect your code they’ll need an intact copy.
You say something goes wrong at index 0
Perhaps if you expand on the term “wrong” that’ll tell you something about what the problem is.
If you instead consider what exactly should happen, and what is currently happening, what is the difference? And, is that something you can adjust?
Or maybe you haven’t made any observations about what happens in which case… do that. Before you can start fixing something you’ll need to observe what that something is. Narrow it down to which specific operation isn’t behaving as you meant.

Ok- What I said was that the loop seems to fire once, for the array at index 0. It

then breaks. It shouldn’t break until it iterates thorough all the arrays in a nested array, in this case indexes 1-14. I don’t know why That is. Someone mentioned I might need to change my index variable. I had tried that at one point and it seemed to not solve the problem, but I can always try again.

weirdly, the loop iterates through the entire nested array if no if conditional is added. for some reason, the if conditional I have added stops the array.

However, the conditional works fine if I use the .foreach iterator instead of writing one out. I have no idea why that could be.

maybe not. but you can observe.
what things can cause a loop to exit?
where exactly does it exit, what was the last thing that happened?

make your program write out what it’s doing and compare to what it should be doing

it’s only a grand mystery if you’re not looking at what’s being done.

chef bob cooks pizza, chef bob serves hamburger. where’s the pizza, what went wrong? watch chef bob cook.

The only thing I have that could cause the loop to exit would be the return statement in the helper function but I’m not sure why that would cause the loop to break because, well, its only validating something and isn’t actual part of the main loop. There’s a return statement at the end of the function but that’s not part of the loop, so it shouldn’t have any effect on the loop either. The last thing it does is work perfectly for index zero. In some tests, I removed the end return statement and didn’t make much of a difference.

Weirdly, restructuring the function to use a forEach iterator worked great, and I’m not sure why, because a foreEach iterator is just shorthand for a loop correct?

Maybe you could suggest a specific test? I feel like you might be under the impression that I haven’t been fighting with this for hours and trying different inputs before asking for help.

Yes my perspective is different from yours. If it takes a couple of hours that’s entirely beside the point in my view. Are you going to pile on more language features before you’re in control of the current ones? Maybe this is the place to be for a little while.

When you are writing code you’re describing some events that you wish to happen.

You need to exert a high degree of control over your program. If you’re not observing individual operations in it then you’re willfully blind, really.

So the problem as far as I see it, isn’t about finishing the task but using the task as a way to practice methods of gaining control over your code.

What you’ve posted is all mangled. It’s not possible to copy it and get the same things to happen as happen on your end. I don’t know what your code is. I can’t tell you what’s wrong, I can’t tell you what specifically to test.
I’m not sure I’d want to either.

If you have some big tangle and you don’t know what it does, that you wrote… then yeah. Spend some time wrestling it down, demand to know all that happens in it.

So you’d check the input of the function. Maybe it’s a size one list, that would make the loop run once.
Then you’d check the loop, maybe you’d comment out all its content, and print out the values it iterates through. Are those the things that should be iterated through?

Those are things you rely on. So you’d check them. What else do you rely on? Being able to tell what to do with a value. So you’d add your conditions back, with no code in them, and inside them write out which one it hit, and you’d look at that and see if that’s okay.

There’s nothing stopping you from finding out exactly where the loop ends, all it takes is to make sure something is written out before and after each statement and then run it and look at the output to see where the loop stopped.

Maybe it turns out your return statement isn’t outside the loop. Maybe it iterates all the way through. Those would be observable things, if you look at them you’ll see the problem, whatever it may be. It may not be fully solved, but identifying the problem is a necessary first step.

Anyway. If I was to look at your code…
Which is this:

function validateCred(array){
let checkSum=0;
let reverseArray=array.reverse();
for ( i= 0; i < reverseArray.length; i++)
{if (i % 2 === 1 && 2reverseArray[i] >= 10)
{checkSum += (2reverseArray[i])-9;}
else if
(i % 2 === 1 && 2reverseArray[i] < 10)
{checkSum += 2reverseArray[i];}
else if (i % 2 === 0)
{checkSum += reverseArray[i];}}
if (checkSum % 10 ===0)
{return true;}
{return false;}}

It won’t run at all.
But sure, I’ll add the multiplication operators back in and then, the problem was that the loop only makes one iteration.
So, print out i from the loop and see if that’s the case then.
after fixing formatting, inserting missing * operators, adding console.log in the loop and calling the function:

function validateCred (array) {
  let checkSum = 0
  let reverseArray = array.reverse()
  for (i = 0; i < reverseArray.length; i++) {
    if (i % 2 === 1 && 2 * reverseArray[i] >= 10) {
      checkSum += (2 * reverseArray[i]) - 9
    } else if (i % 2 === 1 && 2 * reverseArray[i] < 10) {
      checkSum += 2 * reverseArray[i]
    } else if (i % 2 === 0) {
      checkSum += reverseArray[i]
  if (checkSum % 10 === 0) {
    return true
  } else {
    return false




um. well. so the loop does run. what was the problem again?

So… I went through and used someone elses suggestion, who provided me with a missing piece I didn’t know. I updated my code and now it works perfectly the way I originally wrote it without the for each iterator. Thanks for your help.

The problem was it ran once because when I wrote it originally I didn’t realize that i was a global variable and needed to be re-assigned for each loop. Whatever. It works.

While that is wrong, that’ll have no effect unless you’re modifying it somewhere else as well while your loop is running.

I’m not sure what the ■■■■ happened to the operators. They were there when I copied it, otherwise the thing wouldn’t have worked at all.

Well for future reference, please make sure that your code can be copied and executed with zero modification to reproduce what you describe, otherwise there’s no point. Make sure it’s complete as well, a full program.

The i variable is something you could have been printing out, since that is what controls your loop. At the end of your loop, where it would “break”, you could investigate the conditions for it to continue, you’d find that i was too high, and after further narrowing it down you could find that your other function was causing i to change.