How to print this pyramid pattern?

I want this pattern, what’s the logic behind this one? There are lots of blogs online that show code but not teach technique. Share any blogs that you know that teach the logic behind it as well.

At i=1, j=5 should be printed as star, rest as white spaces

At i=2, j=4,6…

At i=3, j=3,5,7….

At i=4,j=2,4,6,8…

At i=5, j=1,3,5,7,9…

How do I convert this into logic?

The whole point is figuring out a process to get that result.

Do you see any patterns on the number of spaces on each line?

> for (i = 1; i <= 5; i++) {
>   for (space = 1; space <= 5 - i; space++) {
>     document.write("0");
>   }
>   for (j = 1; j <= i; j++) {
>     document.write("X0");
>   }
>   document.write("<br>");
> }

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.