Member-only story
JavaScript Algorithm: Looping A Triangle
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: