Jump to content


  • Content Count

  • Joined

  • Last visited

Community Reputation

0 Neutral

About bigPiner

  • Rank
  1. That makes sense and it explains the different results I get with my modifications. I appreciate the need to stay away from document.write--very dangerous. Thank you.
  2. Hello, I am trying to learn javascript using the W3Schools tutorial. I have come across a lesson on for loops that I do not understand. Can someone point me to a place to get an explanation or maybe explain this to me. I want to understand so I can write the code, not just copy and paste someone else's. If this is not the appropriate place to post questions regarding the tutorial, I apologize in advance, please point me in the right direction. Here is the example given in the tutorial: First: <!DOCTYPE html><html><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(){x="";for (var i=0;i<5;i++) { x=x + "The number is " + i + "<br>"; }document.write(x);}</script> </body></html> Second: <!DOCTYPE html><html><body> <script>cars=["BMW","Volvo","Saab","Ford"];for (var i=0;i<cars.length;i++){document.write("My car is a " + cars + "<br>");}</script> </body></html> So my question revolves around the string variable x in the first example. Why is this needed? it is not required when iterating through the cars array in the second example. If I remove the x in the x=x + .... code the function outputs 4--the last number in the condition. if I substitute [document.write(i + "<br />");] or [document.write(x + i + "<br />");] for the x=x + ..bit of code to execute, the output is 0 in either case. If I change the x=""; to x="something"; then that (something) appears at the very beginning of the first line of output but does not repeat at the beginning of each line up to 4 as I would expect. Help, what am I missing? Thank you, bigPiner.
  • Create New...