-
-
Save jonurry/0b22397d957c9cd42c24018e537a8135 to your computer and use it in GitHub Desktop.
for (var triangle = "#"; triangle.length <= 7; triangle += "#") | |
console.log(triangle); |
In the spirit of “there’s no such thing as a stupid question”
Why does the one below work:
for ( var triangle = “#”; triangle.length <=7; triangle += “#”)
console.log(triangle);
When the next one doesn’t? I transferred the for loop into a while loop and expected the outcome to be the same. But instead of starting at 1 # it starts at 2.
let triangle = “#”;
while (triangle.length <=7) {
triangle += “#”;
console.log(triangle);
}
This is my first outing into JavaScript and I’m starting to think it isn’t for me.
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 = "";
Thank you so much for your help. That is shockingly simple.
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)
counter="#";
for(let number=0;number<7;number++){
console.log(counter);
counter+="#";
}