Member-only story

JavaScript Algorithm: Looping A Triangle

Creating a half triangle

Max N
1 min readJun 22, 2022
Photo by Tamanna Rumee on Unsplash

Today we are going to write a program that will console log a half triangle. The half triangle will need to be seven rows tall and consist of nothing but the # character.

Here is an example:

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

That is what your program should output. This won’t be a function so there won’t be any arguments or return statements.

To begin, we will write a for-loop. In this for-loop we want the loop to print out # character but after each loop, the # string will grow by one character.

We do this seven times.

for (let line = "#"; line.length < 8; line += "#") {
console.log(line);
}

We console log each line giving us the exact half triangle from the example above.

If you find this algorithm helpful, check out my other JavaScript algorithm solution articles:

--

--

Max N
Max N

Written by Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.

No responses yet