[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Create a triangle

Posted on 2016-09-19
9
Medium Priority
?
55 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 20

Assisted Solution

by:n2fc
n2fc earned 400 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 600 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 60

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 60

Accepted Solution

by:
Julian Hansen earned 1000 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 60

Expert Comment

by:Julian Hansen
ID: 41808097
You are welcome
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…
Suggested Courses

829 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