Home > Blockchain >  How to mirror this triangle made using nested for loops
How to mirror this triangle made using nested for loops

Time:11-03

How do I mirror this triangle made with nested for loops ?

for($x=0; $x<10; $x  ) { //down
  echo '<br>';
  for($y=0; $y<$x; $y  ) {
    echo '*';
  }
}

I have managed to do this but I can't understand it

for($x=1;$x<8;$x  ){
    for($y=8;$y>=$x;$y--){
        echo"&nbsp;&nbsp;&nbsp;";
    }
    for($z=1;$z<=$x;$z  ){
        echo"* ";
    }
echo"<br>";
}

CodePudding user response:

I'm going to rewrite your first one to use some variables that will hopefully make more sense for the changes that we'll introduce. They're aren't wrong, this just helps (at least me) think about it. Also, instead of an HTML <br /> I'm using a newline, instead of a space I'm using a plus sign, and instead of an asterisk, I'm using a pound sign, just to make it obvious what is going on.

First example

const LINES = 10;

for ($rows = 0; $rows <= LINES; $rows  ) {
    for ($stars = 0; $stars <= $rows; $stars  ) {
        echo '#';
    }
    echo PHP_EOL;
}

Result

#
##
###
####
#####
######
#######
########
#########
##########
###########

Demo here: https://3v4l.org/YpYvS

Second example - extra whitespace to the right

This example is the same as above, however I'm adding some extra white space at the end of the star. The result looks the same as the previous, however if you select the text you'll see those spaces.

const LINES = 10;

for ($rows = 0; $rows <= LINES; $rows  ) {
    for ($stars = 0; $stars <= $rows; $stars  ) {
        echo '#';
    }
    for ($spaces = LINES; $spaces >= $rows; $spaces--) {
        echo " ";
    }
    echo PHP_EOL;
}

Result

#           
##          
###         
####        
#####       
######      
#######     
########    
#########   
##########  
########### 

Demo here: https://3v4l.org/DG34B

Third example - mirroring

To mirror this, we just need to move the whitespace (pluses in my example), before the stars, so just swapping the order of the inner loop:

const LINES = 10;

for ($rows = 0; $rows <= LINES; $rows  ) {
    for ($spaces = LINES; $spaces >= $rows; $spaces--) {
        echo " ";
    }
    for ($stars = 0; $stars <= $rows; $stars  ) {
        echo '#';
    }
    echo PHP_EOL;
}

Result

           #
          ##
         ###
        ####
       #####
      ######
     #######
    ########
   #########
  ##########
 ###########

Demo here: https://3v4l.org/H25Bf

Fourth example - both sides

If you want to show both sides of the pyramid, you can just keep duplicating things

const LINES = 10;

for ($rows = 0; $rows <= LINES; $rows  ) {
    for ($spaces = LINES; $spaces >= $rows; $spaces--) {
        echo " ";
    }
    for ($stars = 0; $stars <= $rows; $stars  ) {
        echo '#';
    }
    for ($stars = 0; $stars <= $rows; $stars  ) {
        echo '#';
    }
    for ($spaces = LINES; $spaces >= $rows; $spaces--) {
        echo " ";
    }
    echo PHP_EOL;
}

Result

           ##           
          ####          
         ######         
        ########        
       ##########       
      ############      
     ##############     
    ################    
   ##################   
  ####################  
 ###################### 

Demo here: https://3v4l.org/E1oqK

  •  Tags:  
  • php
  • Related