# Triangle patterns help needed

hi

I created a function that takes a number as input and prints out a hashed triangle. I am getting the right output when i type in a positive number but the instructions want me to type in a negative number as well. The negative number prints nothing.

Here is my code so far:

function triangle(number) {

var lines = "";

if(number > 0) {

for(var i = 1; i <= number; i ++) {

for(var j = 1; j <= i; j ++) {

lines = lines + "#";

}

lines = lines + "\n";

}

}

if(number < 0) {

for(var i = 1; i <= number; i ++) {

for(var j = number; j >= i; j --) {

lines = lines + "#";

}

lines = lines + "\n";

}

}

return lines + "\n"

}
console.log(triangle(4))
//4 outputs
*
**

which is correct. (hashes dont show up on this page for some reason so i just typed stars)

I tried all kinds of conditionals but still nothing appears in the console when i pass in a negative number as a argument. When the argument is a negative number the triangle should be upside down.
console.log(triangle(-4)) should print:

**

• (hashes dont show up on this page for some reason so i just typed stars)
but nothing prints in my console.

Please help me solve my conditional error.

I’ll get you started. When number is less than 0 you have the following for loop:

for(var i = 1; i <= number; i ++) {

Let’s put real numbers in there. Let’s say the number passed in is -4. We know that i is initialized to 1. So the first time the for loop tries to execute, what are the values in i <= number? Is it true or false? You know you’ll need 4 lines of output (since the number passed in is -4), so can you think of a way to tell the for loop to iterate 4 times using the negative number that was passed in.

I am really lost on this one. Been trying for hours lol

You are close, you don’t need to change much. Think about this again. If you passed in -4 then you need 4 lines of output (i.e. loop through the outer for loop 4 times). And if you passed in -10 then you would need 10 lines of output. Do you see a pattern here? What is the difference between the negative number being passed in and the number of times you iterate through the for loop?

the iterator variable i should equal the number variable or i should decrement? Thats still not giving my desired output lol

for(var i = 1; i <= number; i ++) {

You were so close the first time, you just need to make one simple change. This for loop is counting up from 1. If you passed in -4 then you would want 4 lines of output, which means you want to iterate through this loop 4 times, in other words, keep looping until i is no longer less than or equal to 4. But number is -4. What could you do to make the right side of that comparison 4 instead of -4?

i tried >= number but still no desired output. My console is either blank or gives me fatal error with a bunch of other weird crap lol

I will give you another hint:

for(var i = 1; i <= number; i ++) {

You do not need to make a change to any of the above, you only need to add something. Again, when number is set to -4, what can you do to make it positive (4) so that the for loop works correctly.

Sorry my browser was not responding. I honestly have no clue at this point lol. My brain is fried from the last few hours

Mathematically, do you know how to turn a negative number into a positive number? That’s what you need to do here.

i got the correct output now , i declared the number variable to positive on top of the outer loop. thanks so much, if i count the hours i have been on this problem for 4 hours lol

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.