nolan_1184 Posted October 15, 2012 Share Posted October 15, 2012 (edited) Hi, please refer to this script for my question:-----------------------------------------------------------------------------------------------------------------------------<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>---------------------------------------------------------------------------------------------------------------------------it is a for loop question....1) since x="", then what is value of x in the second loop , third loop and so on. 2) even if x is given a numeric value, it is displayed in the first loop and that's it...not in the other loops...??? y? 3) if x="sss"...just to check the behavior of x....i got the o/p as sssThe number is 0The number is 1The number is 2The number is 3.....so on y "sss" is not addded to the remaining loop pls help...even loop by loop explanation of for loop with help me a lot in understanding it better. thanks Edited October 15, 2012 by nolan_1184 Link to comment Share on other sites More sharing options...
dsonesuk Posted October 15, 2012 Share Posted October 15, 2012 1st loop x=x + "The number is " + i + "<br>"; ( "" The number is 0 <br>) 2nd loop x=x + "The number is " + i + "<br>"; ( "" The number is 0 <br>The number is 1 <br>) 3rd loopx=x + "The number is " + i + "<br>"; ( "" The number is 0 <br>The number is 1 <br>The number is 2 <br>) and so on x is overwitten by the x variable current text value it stores, plus the new for loop text and variable value added to the end of it. 1 Link to comment Share on other sites More sharing options...
nolan_1184 Posted October 15, 2012 Author Share Posted October 15, 2012 (edited) Dear dsonesuk Thankyou very much for the answer. You have cleared my doubts...I m just new to javascript and was stuck in this. Thankyou very much once again. I appreciate your help. :) Edited October 15, 2012 by nolan_1184 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