Chapter 10 Exercises: Functions (Isosceles Triangle and Diamonds)

I completed the exercises in Chapter 10, but the console outputs for my makeIsoscelesTriangle and makeDiamond functions produce logic errors. The lengths for both outputs are correct (ex. console.log(makeIsoscelesTriangle(5).length is 50). Yet, an empty line is printed at the end of the makeIsoscelesTriangle and two empty lines are printed at the end of my makeDiamonds.

All of my other functions in the exercises print correctly without blank lines. I’m worried that my functions are wrong because I used return statements instead of console.log(). I didn’t use console.log() because undefined printed after everything else printed correctly. Also, I set if-then conditional statements to add “\n” only in certain circumstances. I know that the instructions stated not to add “\n” to the end of the string. Yet, with the conditions set, I did not experience any logic errors until now.

Is there a way to use both console.log() and “n”? Will I need to refactor my previous functions?

Hey Artistic,

for the diamond, you used one loop to make each line a combination of blank spaces and characters right?
then you used a loop to count the lines? Counting up, something like this:
for(k=0; k < height; k++)

for each loop use a different letter (i, k, l, etc) so you can see which loop you are running.
I needed 4 loops total (one to count blank spaces, one to count characters, one to control the growing triangle, and one to control the shrinking triangle)

so when you are making the triangle, check to see if you are at the last line or not (height - 1).

Then, without a line break start the next triangle, but this time count in reverse:
for(l=height-1; l >= 0; l–)

in this case, since you are counting backwards, as long as this loop is not zero add the line break.

if the loop counter is zero, you are on the last line, so this one doesn’t get the (/n).

Hope that helps, play around some and see if you can get it. I can send you some code snippets if you need, but the learning comes from playing around with it and understanding it yourself. However, that being said, if you are spending hours on it and still need help, then feel free to ask, there is a fine line between learning and being hopelessly lost.

Good Luck.

1 Like


I wouldn’t worry about the undefined message in the console. Just write it off as a pecularity of the JavaScript console. Judging from your screenshots, it’s all working correctly except for the extra blank line after the initial triangle.

I didn’t quite follow what dconford was recommending, but the solution needs only one for loop in the makeIsoscelesTriangle function and one loop in the makeDiamond function. There are two ways to implement the makeDiamond function:

  • make use of makeIsoscelesTriangle function. In other words, call makeIsoscelesTriangle from within makeDiamond; OR
  • use two string variables (one for the top triangle; the other for the bottom one) within the same loop. The code will repeat all the code from makeIsoscelesTriangle plus have an extra line within the for loop which will simulteneously build the bottom triangle.

I’m hoping this makes sense. :sweat_smile:

1 Like

For the makeDiamond function, I used one empty string variable to hold the diamond. Then, I concatenated the result of the makeIsoscelesTriangle function with that string. I used the reverse function from Chapter 10 to concatenate the reverse of the makeIsoscelesTriangle.

So far, I haven’t used more than one for loop for my functions in Chapter 10’s exercises. I used one or more if-else statements in my for loops.

I’ll try to refactor my code and see if I can figure it out. Thank you for your help.

1 Like

I used one loop in the makeIsoscelesTriangle function and one for the makeDiamond function as well.

I implemented my makeDiamond by using the makeIsoscelesTriangle function, but I also used the reverse function from Chapter 10. I don’t know if using the reverse function was optional or required, though.

UPDATE: I was able to fix both of my functions by adding another condition in my if statement for my makeIsoscelesTriangle function. For the makeDiamond function, I still concatenated strings with "\n". Both functions are printing properly now. Thank you both for your help!

I believe it was optional, and I think you were supposed to get incorrect or suboptimal results. I got a lopsided “diamond” (because the spaces ended up on the wrong side of each line).


As an exercise, I modified the original makeIsoscelesTriangle function to add spaces on both sides. Then using the reverse function gave me a correctly shaped diamond. But to further reflect on the results: While you achieve code reuse (the upside), it ends up being an inefficient approach which also produces potentially undesired side effects (the downside). That’s probably what they mean by “consider what happens” when you use this approach.

Alternatively, you could probably use the slice function (or the substring function) to delete the last unneeded new line character: myString.slice(0, -1)

1 Like

Interesting. I utilized the makeSpaceLine function for my makeIsoscelesTriangle function to print the triangle. I may revisit my code later to try different approaches.

Interesting indeed. I actually jumped right into the makeIsoscelesTriangle part of the Exercises and didn’t realize they had you do the makeSpaceLine function in a previous step. Code reuse is a good hunch, and they guided you that way in previous steps. But there is no such guidance here. And the “Console Output” is NOT showing trailing spaces on the right side. I don’t know if it was LaunchCode’s intention, but it sparked this healthy discussion.

1 Like

Yeah, I agree that the other steps provided more hints of code reuse. This exercise did spark healthy discussion and exploration :slightly_smiling_face:!