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 Feb 16, 2018

Program Structure

2.1 Looping a triangle

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

// #
// ##
// ###
// ####
// #####
// ######
// #######

It may be useful to know that you can find the length of a string by writing .length after it.

var abc = "abc";
console.log(abc.length);
// → 3

Most exercises contain a piece of code that you can modify to solve the exercise. Remember that you can click code blocks to edit them.

@jonurry
Copy link
Author

jonurry commented Feb 16, 2018

Hints

You can start with a program that prints out the numbers 1 to 7, which you can derive by making a few modifications to the even number printing example given earlier in the chapter, where the for loop was introduced.

Now consider the equivalence between numbers and strings of hash characters. You can go from 1 to 2 by adding 1 (+= 1). You can go from "#" to "##" by adding a character (+= "#"). Thus, your solution can closely follow the number-printing program.

@robparra0210
Copy link

var x='';
for(let i=0; i <8; i++){
x= '#'+ x;
console.log( x );
}

@barrosfilipe
Copy link

Just tried without using a for loop

Array(7).fill().map((_, i) => '#'.repeat(i + 1)).join('\n');
// "#
// ##
// ###
// ####
// #####
// ######
// #######"

@noopy-ui
Copy link

counter="#";
for(let number=0;number<7;number++){
console.log(counter);
counter+="#";
}

@GrahamKay
Copy link

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.

@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