Solved

Create a triangle

Posted on 2016-09-19
9
36 Views
Last Modified: 2016-09-20
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

0
Comment
Question by:Isaac
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 19

Assisted Solution

by:n2fc
n2fc earned 100 total points
ID: 41805637
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
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 150 total points
ID: 41805664
You can make this change to get the for working... change:
for(x="#"; x<=num; x+="#"){

Open in new window

to:
for(x="#"; x.length<=num; x+="#"){

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41806242
What sort of a triangle are you after
This
#
##
###
####
#####
######
#######

Open in new window

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

Open in new window

0
 
LVL 5

Author Comment

by:Isaac
ID: 41806640
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 19

Expert Comment

by:n2fc
ID: 41806667
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
 
LVL 5

Author Comment

by:Isaac
ID: 41806684
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
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 41806815
Going back to your original question.

This is the reference code
for (var line = "#"; line.length < 8; line += "#")
  console.log(line);

Open in new window

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;
}

Open in new window

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>

Open in new window

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>

Open in new window

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
 
LVL 5

Author Closing Comment

by:Isaac
ID: 41807941
Thanks for all the help.
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41808097
You are welcome
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now