+ 1
What's the difference between these While Loop Output knowing that the two Combilers are outside the Loop ?
what is the difference between these two examples Example 1: var x; while (x<6) { x++; } document.write(x); OUTPUT : 6 Example 2 : var text = ""; var i = 0; while (i < 10) { text += "<br>The number is " + i; i++; } document.getElementById("demo").innerHTML = text; OUTPUT : The number is 0 The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 The number is 6 The number is 7 The number is 8
2 ответов
+ 2
first example is simple JavaScript..
and the second example is using DOM model.. it takes an id of demo.. and prints it in the DOM...
l the html markup could have been like..
<html>
<body>
<p id ="Demo">
</p>
</body>
<script>
//here the above example 2 script goes on...
</script>
</html>
................
and another difference is that in first example the document.write is after the block of while loop.. so when the value of x has incremented to 6 then the loop condition fails.. and the output prints to 6..
but in the second example the there is a variable text which stores the value as it increments and checks the conditions and, when the loop's condition fails.. the text variable holding the set of output gets printed
+ 1
Thanks alot.