Guest User

Untitled

a guest
Jan 7th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //write a function printTriangle, accepts
  2. //a height as parameter.
  3. //assuming equilateral triangles
  4. private function printTriangle(height:uint):void
  5. {
  6.     if (height == 0)
  7.     {
  8.         trace('Nothin');
  9.         return;
  10.     }
  11.     var width:uint = (height - 1) * 2 + 1; //height - 1 because height of 0 is a single x's.
  12.     var outStr:String = "";
  13.     for (var h:uint = 0; h < height; ++h)
  14.     {
  15.         //a line consist of h*2 + 1 number of x's because one x for center, then h number of x's on each side.
  16.         //Rest of space is left for O's. We have Width number spots, take away number of x's we have the o's.
  17.         //write 0s, then write xs, then write rest of 0s.
  18.         var numOfXs:uint = h * 2 + 1;
  19.         var numOfOsHalf:uint = (width - numOfXs) / 2;
  20.         //draw symbol o's
  21.         outStr += drawSymbol('O', numOfOsHalf);
  22.         outStr += drawSymbol('X', numOfXs);
  23.         outStr += drawSymbol('O', numOfOsHalf);
  24.         outStr += "\n";
  25.     }
  26.    
  27.     trace(outStr);
  28.    
  29.     function drawSymbol(symbol:String, count:uint):String
  30.     {
  31.         var oStr:String = new String();
  32.         for (var c:uint = 0; c < count; ++c)
  33.         {
  34.             oStr += symbol;
  35.         }
  36.         return oStr;
  37.     }
  38. }
Add Comment
Please, Sign In to add comment