# Create a triangle

Hi,

Can someone please explain why the code below does not work like the last 2 lines that's commented out?
IT's supposed to create a triangle.

``````
<script>
//Write a loop that makes seven calls to console.log to output a triangle.

var num=0;
var pound;
while(num <= 7){
//console.log("<strong>"+num+"</strong>");
for(x="#"; x<=num; x+="#"){
pound += "#";
console.log(pound);
}
num +=1;
}

//for (var line = "#"; line.length < 8; line += "#")
//  console.log(line);
</script>
``````
LVL 5
SharePoint Client Side DeveloperAsked:
###### Who is Participating?

Commented:
Going back to your original question.

This is the reference code
``````for (var line = "#"; line.length < 8; line += "#")
console.log(line);
``````
Quite simple - creating a string with a value of '#', looping around adding a '#' to the string on each iteration until the length of the string is 8.
Results in successively longer lines (by 1 # each time)

The question is - what are you trying to achieve with the rewrite.
You have an outer while statement which is the same as a for loop just using num as a counter to work out when rows are done. However, you have an internal loop that is adding to the complexity of this solution. Lets look at that
``````while(num <= 7){
for(x="#"; x<=num; x+="#"){
pound += "#";
console.log(pound);
}
num +=1;
}
``````
What is happening here is you are actually building 2 strings.
each time the for loop goes around x is growing by 1 # - and on each iteration of the while loop x is being reset to start.
BUT
You are not outputting x - you are maintaining another string (pound) in the loop - which grows continually because you are never resetting it on the next iteration of the while.
So in answer to your question - there are two strategies to make your code work
``````<script>
var num = 0;
while(num <= 7){
for(x="#"; x.length<=num; x+="#");

console.log(x);
num +=1;
}
</script>
``````
In this version we use the for loop to create the output string - the only change is the recommendation by Terry Woods to make x<num to x.length < num.
The second approach - using the pound string is as follows
``````<script>
var num = 0;
while(num <= 7){
var pound = '';
for(x="#"; x.length<=num; x+="#"){
pound += '#';
}

console.log(pound);
num +=1;
}
</script>
``````
Note in line 4 we are resetting pound back to the empty string on each iteration - I think this is what was tripping you up - you only initialised pound before the while and as a result you ended up with a 21 character string in the seventh row.
0

Commented:
1) in line 6, you never set initial value for pound

2) in line 9 you compare a string (x) to an int (num)...
0

IT GuruCommented:
You can make this change to get the for working... change:
``````for(x="#"; x<=num; x+="#"){
``````
to:
``````for(x="#"; x.length<=num; x+="#"){
``````
0

Commented:
What sort of a triangle are you after
This
``````#
##
###
####
#####
######
#######
``````
Or this
``````      #
###
#####
#######
#########
###########
#############
``````
0

SharePoint Client Side DeveloperAuthor Commented:
The first one.  I'm actually just doing an exercise from the "Eloquent JavaScript" book.  They provided a solution but I was trying to come up with another way.

Terry Wood's code worked but it gave me more than 7 lines.  It's supposed to be a 7 line triangle.  Sorry for not noting that in my question.
0

Commented:
1) I gave you areas to look at to get you going...

2) Do you want to LEARN or do you just want to be spoon-fed a solution?   Obviously, the solution in the book DOES work!
Yours needed a "little help"...  Most experts here can correct your code for you, but you WON'T learn much from that!

3) The best way to learn is by tweaking your code and observing what happens, to figure out your mistakes... If you are STUCK, and need a clue, those here will be happy to guide you...  If you just want an answer to the puzzle, you can get that also... BUT YOU WON'T BE LEARNING!

Just some friendly advise!
0

SharePoint Client Side DeveloperAuthor Commented:
n2fc,

I picked up a book on my own, took my time to go through the chapter and looking forward for more, and struggled to get a solution before coming to EE.  Of course I want to learn or I would not have come up with my own code.

It would have been rude not to respond to his question.

Thanks and advise appreciated!
0

SharePoint Client Side DeveloperAuthor Commented:
Thanks for all the help.
0

Commented:
You are welcome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Already a member? Login.

All Courses

From novice to tech pro — start learning today.