Jump to content
shaijuelayidath

For Loop explanation

Recommended Posts

In the For Loop tutorial of w3si did'nt understand some code line of the following js code, i indicated it below ------------------------------------------------------------------------------------------------------------------------<button onclick="myFunction()">Try it</button><p id="demo"></p> <script>function myFunction(){var x="",i; <---[ what this type of var means, and wat this "" means, ]for (i=0;i<5;i++) { x=x + "The number is " + i + "<br>"; <---[ What the x=x means] }document.getElementById("demo").innerHTML=x;}</script>------------------------------------------------------------------------------------------------------------------------Actually i tried and found it can be write another method, but why w3s written the above indicated method, and not mentioned anywhere about this kind of for loop method.

Share this post


Link to post
Share on other sites

var is what you use to declare variables in JavaScript. The "" means: set variable(var) x to an empty string. x = x means: Each time through each loop, variable x will be concatenated with: x + "The number is " + i + "<br>"; In other words, when it's done, x will look like this: "The number is 0<br>The number is 1<br>The number is 2<br>The number is 3<br>The number is 4<br>. When x is then set to the innerHTML of "demo", it will look like this:The number is 0The number is 1The number is 2The number is 3The number is 4

Share this post


Link to post
Share on other sites

In this case, x is a string. On each iteration of the loop more content is added to the end of the string. x = (x + "string") means that you're adding more content to the value of x and then assigning that value to x.

Share this post


Link to post
Share on other sites

fyi, if you go in order, the tutorials on variables comes many lessons before loops. http://www.w3schools.com/js/js_variables.asp I would advise to progress through the tutorials in order.

Share this post


Link to post
Share on other sites

my question related to this script: <body> <p>Click the button to loop through a block of code five times.</p><button onclick="myFunction()">Try it</button><p id="demo"></p> <script>function myFunction(){var x="",i;for (i=0;i<5;i++) { x=x + "The number is " + i + "<br>"; }document.getElementById("demo").innerHTML=x;}</script> </body>----------------------------------------------------------------------------- i need help in understanding the "for" loop....my questions are as follows: 1) x=x+"The number is"+i -----over here assuming that the value of x is "" (empty string),,,vn the loop starts does the value of x changes in very loop.....i was stuck in this statement...i even tried declaring x="ss" just to check the behavior of x...the o/p was :ssThe number is 0The number is 1The number is 2The number is 3The number is 4 ....well more question rises from here....like why javascript runs the value of x only in d first loop and not in second, third..so on. ..anyone willing to explain the behavior step by step for every loop steps will be highly appreciated...thanks.

Edited by nolan_1184

Share this post


Link to post
Share on other sites

it's hard to tell what your specific question is but, it sounds like you are confused about how this line is working

x=x + "The number is " + i + "<br>";

while it's true x is initalized as an empty string, it is only for best practice, since we over write its value everytime. We simply add the current value of x, plus a string to the so everytime we go through the loop. x = "ss";1) {ss} = (ss) + "The number is " + 0 + "<br>";2) {ssThe number is 0<br>} = (ssThe number is 0<br>) + "The number is " + 1 + "<br>";3) {ssThe number is 0<br>The number is 1<br>} = (ssThe number is 0<br>The number is 1<br>) + "The number is " + 2 + "<br>";4) {ssThe number is 0<br>The number is 1<br>The number is 2<br>} = (ssThe number is 0<br>The number is 1<br>The number is 2<br>) + "The number is " + 3 + "<br>";5) {ssThe number is 0<br>The number is 1<br>The number is 2<br>The number is 3<br>} = (ssThe number is 0<br>The number is 1<br>The number is 2<br>The number is 3<br>) + "The number is " + 4 + "<br>"; So you never reset the value of x, you just keep overwriting it by adding itself, and the new string form the loop. alternately, you could just write your loop like this

for (i=0;i<5;i++){  x += "The number is " + i + "<br>";}

perhaps reading through the operators and variables sections of the tutorials might be of help to you.http://www.w3schools...s_variables.asphttp://www.w3schools...s_operators.asp

Edited by thescientist

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...