Jump to content

Javascript doubt


nolan_1184

Recommended Posts

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 by nolan_1184
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...