How to make a Triangle loop?

The prompt is this:
Write a loop that makes seven calls to console.log to output the following triangle:

'#
'##
'###
'####
'#####
'######
'#######

This is how I wrote my code

//Making A Triangle
let triangle = ‘#’ ;
do { (triangle + triangle);
} while (triangle.length !== 7);
console.log(triangle);

So far this just creates a continuous loop and it errors out the page eventually so I have to close the tab. I’ve tried to do variations with explicit but that also failed from how I wrote it.

Link to Prompts Program Structure :: Eloquent JavaScript

Looking at the logic of your do... while loop it seems you have a few syntax errors. The basic structure is:

do {
  // Things you want to do
  //

} while (condition to stop);

And between the brackets you should use valid JS. So (triangle + triangle) is not doing anything. If you are trying to increase the number of # stored inside the string triangle, you need to assign it. It could be something like:

triangle = triangle + triangle;

but keep in mind that once triangle is ## or more, when you add it to it self it will go from 2 to 4 hashtags! Instead, why not try to simply add '#' to the variable every loop iteration? You can first print the value, then add the next #

1 Like

It looks like that worked perfectly once I used your advice and saw the mistakes from my syntax!

//Making A Triangle
let triangle = ‘#’
do {
console.log(triangle);
triangle = triangle + ‘#’;
} while (triangle.length !== 8);

Since it logs at the front end instead of the back end I just extended it to 8 instead of 7, thanks for all the help!

1 Like