3

I'm completely new to coding and have started learning JavaScript recently. I don't understand why the following code causes an infinite loop. Why does the birthday(myAge) function not work within the loop to make the condition (myAge < 23) false?

var myAge = 22
var birthday = function(myAge){
    return(myAge + 1);
}

while (myAge < 23){
    console.log("You're only 22");
    birthday(myAge)
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
JosephByrne
  • 123
  • 7

2 Answers2

7

Because you are not modifying myAge in any way. Your function is simply returning myAge + 1.

Try assigning the return value back to myAge:

while (myAge < 23){
    console.log("You're only 22");
    myAge = birthday(myAge);
}

Or alternatively, if you remove the function parameter, then the name myAge within the function will refer to the global variable, and you can modify it directly:

var myAge = 22
var birthday = function(){
    return (myAge = myAge + 1); 
    // or return myAge += 1;
    // or return ++myAge;
}

while (myAge < 23){
    console.log("You're only 22");
    birthday();        // note, no need to pass any parameters
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • Hi thanks that makes sense. But I don't understand why the 'birthday' function doesn't assign the value back to'myAge'? – JosephByrne Mar 27 '14 at 17:40
  • 1
    @JosephByrne for a couple of reasons. 1) `myAge + 1` doesn't assign any values. It gets the current value and returns that value + 1. 2) The function parameter is different from the global variable, so simply modifying the parameter won't have any effect. – p.s.w.g Mar 27 '14 at 17:43
  • Ok. Is there a way to write a function that does actually modify the variable's value? Or am I just misunderstanding what functions are for? – JosephByrne Mar 27 '14 at 17:47
  • Thanks for your patience, I know this is pretty basic stuff! – JosephByrne Mar 27 '14 at 17:47
  • @JosephByrne See my updated answer for an alternative – p.s.w.g Mar 27 '14 at 17:50
  • Oooh I like that - that's what I wanted to do. I get it now - while I had a function parameter I would only be able to modify that value, but if you remove it the function modifies the global variable. thank you :) – JosephByrne Mar 27 '14 at 17:53
2

You need to increment myAge in some way, or else you will be 22 forever... although that's not such a bad thing. I think you discovered the fountain of youth.

return (myAge += 1)
jOshT
  • 1,525
  • 1
  • 11
  • 15
  • 2
    Putting that inside the function will only modify the local variable (the parameter value), not the global variable. – p.s.w.g Mar 27 '14 at 17:40
  • myAge is a global variable already and not bound to the function scope. – jOshT Mar 27 '14 at 18:12
  • 1
    Look again. In OP's code, there is a parameter named `myAge`, too. So `myAge += 1` will only modify the value of that parameter. You'd either have to assign the return value back to `myAge` outside of the function, or remove the parameter so `myAge` is bound to the global variable. – p.s.w.g Mar 27 '14 at 18:53
  • Correct, oversight on my part – jOshT Mar 27 '14 at 18:55