Link to home
Start Free TrialLog in
Avatar of Isaac
IsaacFlag for United States of America

asked on

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>

Open in new window

SOLUTION
Avatar of n2fc
n2fc
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What sort of a triangle are you after
This
#
##
###
####
#####
######
#######

Open in new window

Or this
      #
     ###
    #####
   #######
  #########
 ###########
#############

Open in new window

Avatar of Isaac

ASKER

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.
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!
Avatar of Isaac

ASKER

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!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Isaac

ASKER

Thanks for all the help.
You are welcome