Skip to content

Instantly share code, notes, and snippets.

@jonurry
Last active October 16, 2023 09:31
Show Gist options
  • Save jonurry/0b22397d957c9cd42c24018e537a8135 to your computer and use it in GitHub Desktop.
Save jonurry/0b22397d957c9cd42c24018e537a8135 to your computer and use it in GitHub Desktop.
2.1 Looping a triangle (Eloquent JavaScript Solutions)
for (var triangle = "#"; triangle.length <= 7; triangle += "#")
console.log(triangle);
@jonurry
Copy link
Author

jonurry commented Oct 27, 2021

Hey @worgraeme - this stuff can be tricky so keep going!
It's because you initialised triangle with a value of # so it already has one hash in it. Then you added another hash in the while loop before logging to the console where you see two #s
To fix this, initialise triangle with an empty string:
let triangle = "";

@GrahamKay
Copy link

Thank you so much for your help. That is shockingly simple.

@code-crasher
Copy link

trying to create one that takes any value and creates a triangle

const loopTriangle = (loopCounter)=>{
let num=' ';
for (let i = 0;i<loopCounter;i++){
num +=" #";

console.log(num);
}
};
if you run the function it will create your desired triangle

//loopTriangle(10)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment