Life Phase error

I am getting undefined in the console when I expect to get one of the 5 different “phases”.

My exercise

My code:

function lifePhase(age) {

var phase = 1;
  
  for (i = 0; i >= age; i++) {
    //Counts until is equal to "age" and adds one to "phase" everytime it meets the phase up conditions.
    console.log(i + ' top');
   if (age === 1) {
     phase = 1;
   }
    
    else if (i === 4) {
        phase = 2;
      }

     else if (i === 13) {
        phase = 3;
      }

      else if (i === 20) {
        phase = 4;
      }

      else if (i === 65) {
        phase = 5;
      }

  //Checks "phase"'s value and returns the name of that phase.

    if (phase === 1) {
      return 'baby';
    }
    
     else if (phase === 2 && i === age) {
          return 'child';
        }

       else if (phase === 3 && i === age) {
          return 'teen';
        }

        else if (phase === 4 && i === age) {
          return 'adult';
        }

        else if (phase === 5 && i === age) {
          return 'senior citizen';
        }

      else if (age < 0 || age > 140) {
        return 'This is not a valid age.';
      }

}
}

console.log(lifePhase(1));

Maybe define “i” as a variable in the for loop?

1 Like

Not a bad shout, but I don’t think JavaScript complains too loudly about not doing for (let index = ...).

In your for loop, what precisely is the purpose of the second parameter inside the brackets? In your case, you’ve written i >= age; - how is this affecting the flow of your program, specifically with regards to execution of that loop? :slight_smile:

Isn’t this just the stop condition? I don’t necessarily want i = age I just do that to make it so I don’t have to write to many if statements. I don’t see how the stop condition is hindering the flow.

You can use printing to find out what value a variable has or when something happens. You can for example print something out from the very first line of your function. Does that run? What should happen next? Print that out too, does that happen? And so on until you’ve had a look at all the things that you need to happen in order to get the final outcome you’re looking for.

You’re stating that your final outcome is wrong, so look at what was done to obtain it. It’s not only the final outcome you can look at.

Yes, I am trying that right now but even right after I declare phase it is undefined. I don’t know what is causing this. I have moved phase’s declaration out of the for statement by the way. This does not help the code in the for statement is still broken.

var res;

function lifePhase(age) {
  
  for (var i = 0; i === age; i++) {
    //Counts until is equal to "age" and adds one to "phase" everytime it meets the phase up conditions.
      var phase = 0;
    console.log(phase);
   if (i === 1) {
     phase++;
   }
    
    else if (i === 4) {
        phase++;
      }

     else if (i === 13) {
        phase++;
      }

      else if (i === 20) {
        phase++;
      }

      else if (i === 65) {
        phase++;
      }
  }
  //Checks "phase"'s value and returns the name of that phase.
console.log(phase)
    if (phase === 1) {
      res = 'baby';
    }
    
     else if (phase === 2) {
          res = 'child';
        }

       else if (phase === 3) {
          res = 'teen';
        }

        else if (phase === 4) {
          res = 'adult';
        }

        else if (phase === 5) {
         res = 'senior citizen';
        }

      else if (age < 0 || age > 140) {
        res = 'This is not a valid age.';
      }


}

lifePhase(8);
console.log(res);

As you can see I am declaring phase then immediately logging it to the console. This is what is shows:

And if you at the same location log out “ELEPHANTS” do you see that?

Because the typical reason why you wouldn’t get something to print is not having called the print function.

Also note the oddity in this use of your function:

lifePhase(0);
console.log(res);

That’s like doing this:

5 + 3
console.log(sum)

I did that because I thought there maybe a problem with the scope but I see that is not the case. Nothing in my for statement is working.

Well, then, maybe start with a loop that counts to 10. Get that working. Or consider what a for-loop does and how you can affect it. But removing all other code and focusing on one small part will give you fewer things that can steal your attention.

1 Like

Does anything look wrong with this?

 for (var i = 0; i === 10; i++) {
   console.log(i);
  }

I don’t know. Or rather, that’s beside the point, the question is how you can find the problem.

So, you know. Run it. Does it reproduce the problem? If so, good, because that would have really narrowed it down.

If you have something sufficiently small then you can compare it to a reference.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for
there are examples there. Do they perform as they should?

It does reproduce the problem, this leaves me clueless. I have no idea why that code would not run.

Got it working, although I don’t know how… I just re-wrote some stuff.

I would very much recommend reading up on for loops. Use the link @ionatan posted and focus on the syntax portion. That was where you made your mistake, but don’t worry, this is just a part of learning to code

Probably worthwhile to compare and give some consideration to how you would have spotted it.
You have a really small piece of code where you are reproducing the problem. MDN’s page on the for-statement has an equivalent (except correct) piece of code, comparing should be trivial.

Using printing to trace what is being done lets you narrow problems down, and when you have a small enough piece then you can go read about it.

Then as @ionatan and @ktsotras have suggested, I would encourage you to read the MDN documentation on loops:

Specifically, this bit:

for ([ *initialization* ]; [ *condition* ]; [ *final-expression* ])
    *statement*  

condition

An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the for construct.

Do you see now why a condition of i >= age was not working, given a value of i = 0 initially?

I think you were looking to have the condition work to mean “run the loop until i exceeds age”, but - for whatever reason - got the comparison backwards. :slight_smile:

Excellent news, in part… It will be excellent news all round when you understand why your revised code works. Hopefully what you’ve changed is the for condition, since that was primarily what was wrong initially, and hopefully the snippet from MDN will clarify why it was incorrect. :slight_smile:

If anything remains unclear, though, please ask and we’ll try and explain. :slight_smile: