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.
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.
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.
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?
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?
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.
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