Drawing a Christmas Tree Using Nested For Loops : For « Language Basics « JavaScript DHTML





Drawing a Christmas Tree Using Nested For Loops

  
<HTML>
<BODY>
<CENTER>
<SCRIPT> 
   var width=1; 
   for (i=0; i <= 5 ; i++) { 
      for (x=0; x<=4; x++) { 
         for (y=1; y<=width; y++) { 
            var Number=Math.random()*10; 
            var Ornament=Math.round(Number); 
            if (Ornament<=1) { 
               document.write("O"); 
            } 
            if (Ornament>=2)  { 
               document.write(" X"); 
            } 
         } 
         document.write("<BR>"); 
         width=width+1; 
      } 
      width=width-2; 
   } 
</SCRIPT>
</CENTER>
</BODY>
</HTML>
           
         
    
  










Related examples in the same category

1.For loop for lines
2.Check the loop counter
3.For loop
4.Using a for..in Loop in JavaScript
5.A Demonstration of a Nested Loop
6.A for Loop Used to Count from 0 to 99
7.Using the label Statement
8.Using the continue and break Statements
9.Use of the for Statement
10.The break Statement
11.The continue Statement
12.Labeled Statements
13.Plain for loop
14.For loop with alert dialog
15.Declare for loop counter
16.Control the loop step
17.Use for loop to fill an array
18.Use for in loop to go through an array