Solved

Create a triangle

Posted on 2016-09-19
9
44 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 20

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 56

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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

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 56

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 56

Expert Comment

by:Julian Hansen
ID: 41808097
You are welcome
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

730 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