shaijuelayidath Posted October 4, 2012 Share Posted October 4, 2012 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. Link to comment Share on other sites More sharing options...
Don E Posted October 4, 2012 Share Posted October 4, 2012 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 Link to comment Share on other sites More sharing options...
shaijuelayidath Posted October 4, 2012 Author Share Posted October 4, 2012 sorry still i did'nt get what the role of 'x', because i'm very new to javascript Link to comment Share on other sites More sharing options...
Ingolme Posted October 4, 2012 Share Posted October 4, 2012 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. Link to comment Share on other sites More sharing options...
thescientist Posted October 9, 2012 Share Posted October 9, 2012 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. Link to comment Share on other sites More sharing options...
nolan_1184 Posted October 14, 2012 Share Posted October 14, 2012 (edited) 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 October 14, 2012 by nolan_1184 Link to comment Share on other sites More sharing options...
thescientist Posted October 15, 2012 Share Posted October 15, 2012 (edited) 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 October 15, 2012 by thescientist Link to comment Share on other sites More sharing options...
dsonesuk Posted October 15, 2012 Share Posted October 15, 2012 ???? some taken same class as http://w3schools.invisionzone.com/index.php?showtopic=45404&pid=253085&st=0entry253085, thought my post had been wiped. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now