DEV Community

Cover image for Triangle with loop
Robertopaolo Ramirez
Robertopaolo Ramirez

Posted on

3

Triangle with loop

Source Code

let myString = "#"
for(let i =0; i <= 7; i++){
  console.log(myString)
  myString += "#"
}
Enter fullscreen mode Exit fullscreen mode

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)
}
Enter fullscreen mode Exit fullscreen mode

Terminal show
input

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 += "#"
Enter fullscreen mode Exit fullscreen mode

Result

Nice!
we already have our triangle with loop

If you have other way to do this triangle, share with us!!

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs