Source Code
let myString = "#"
for(let i =0; i <= 7; i++){
console.log(myString)
myString += "#"
}
Explained
As we already know, the for is a way to be able to execute the same code until the condition is fulfilled.
So, in this case, the logic I concluded is as follows:
Execute a string (in my case I chose #) X times, and for each time it is executed, an extra print will be added.
We start by creating a variable containing the string.
The for
said something like this: start with index = 0
, as long as the index is less than or equal to 7, add 1 to the index.
This will make us loop 7 times until our variable gets the value greater than 7.
Let see
let myString = "#"
for(let i =0; i <= 7; i++){
console.log(myString)
}
Now we need to add another string for each console.log
line. To do this we just need to do a sum of strings in the loop:
myString += "#"
Nice!
we already have our triangle with loop
If you have other way to do this triangle, share with us!!
Top comments (0)