Vodka_Tonic
Vodka_Tonic

Reputation: 152

I'm having trouble understanding this Javascript function

I don't understant how the toAlert variable works here. Why is it assigned two quotation marks? I also don't understand the "toAlert" statement in the for loop block. Why does toAlert = toAlert?

After messing around with the function I wanted to see the effect the variable toAlert has if I were to change it. So I assigned it to

var toAlert;

and it only alerts one line of text as opposed to 5. Can anyone explain this to me?

var runAway = function(){
  var toAlert = "";
    for(var i = 0; i<5; i++){
      toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n";
    }
    alert(toAlert);
  }
}

runAway();

Upvotes: -2

Views: 92

Answers (4)

kayz1
kayz1

Reputation: 7434

var toAlert = "";

That's an empty String. At first the toAlert variable is just an empty String.

toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n";

You're appending "Lions, Tigers, and Bears, Oh My!!\n" to the previous value of the toAlert variable.

toAlert += "Lions, Tigers, and Bears, Oh My!!\n";

You can write it that way.

Upvotes: 0

Bhaskar Melkani
Bhaskar Melkani

Reputation: 331

  var runAway = function(){
   var toAlert = "";
   for(var i = 0; i<5; i++){
      toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n";
   }
   alert(toAlert);
  }


runAway is a function that has a variable named toAlert which is of string type, and then it iterates using for loop and concatenates the runAway string and adds "Lions, Tigers, and Bears, Oh My!!\n" on each iteration. After completion of iteration it alerts the complete string.

And a closing parenthesis is extra in your code.

what exactly did you not get in this ?

Upvotes: -1

CRABOLO
CRABOLO

Reputation: 8793

    var runAway = function(){
    var toAlert = "";         // 1. this is just an empty string. Probably so that that they can customize it to how they want when they call alert(toAlert)
      for(var i = 0; i<5; i++){
        toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n";
      }
      alert(toAlert);
    }
    }

runAway();
  1. For example, the coder could call alert("This is error #424343 in div id #cats"); So that would show up in the alert if that error happens. Then the coder has another alert/error message that he wants to show if there's an error in div id #dogs. So he would type alert("This is error whatever, in div id #dogs"); to call that customized error message for that specific event.

Upvotes: -1

Harsha Venkataramu
Harsha Venkataramu

Reputation: 2914

var toAlert = "" ; indicates that toAlert is a variable of type string

toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n";

is to concatenate the string "Lions, Tigers, and Bears, Oh My!!\n" to the value of toAlert

Inside the for loop ,

the first time, toAlert is empty , and so after the execution of the statement toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n"; , the value of toAlert will be

""+"Lions, Tigers, and Bears, Oh My!!\n"

because + behaves like a concatenation operator in case of strings, it concatenates two 'strings'

The second time , it will be

"Lions, Tigers, and Bears, Oh My!!\n" + "Lions, Tigers, and Bears, Oh My!!\n"

If you need to append the string 5 times , your code would be

var runAway = function(){
    var toAlert = "";
    for(var i = 0; i<5; i++)
    {
        toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n";
    }
    alert(toAlert);
}

runAway();

Upvotes: -1

Related Questions