Javascript triangle output problem

Javascript triangle output problem
0

#1

Hello guys.I have this code below which has an output of the triangle.

function makeLine(length) {
var line = "";
    for (var j = 1; j <= length; j++) {
    line += "* ";
    }
    return line + "\n";
}

 function buildTriangle(length) {
 var space = "";   
  
  for(i=1;i<=length;i){

  space += makeLine(length--);
  }

  return space.split("").reverse().slice(1).join(""); 
  
}
console.log(buildTriangle(10));

output:

 *
 * *
 * * *
 * * * *
 * * * * *
 * * * * * *
 * * * * * * *
 * * * * * * * *
 * * * * * * * * *
 * * * * * * * * * *

but I have an extra space before the beggining of the line. How to delete this extra space at the beginning of each line?


#2

The .reverse() is the culprit. When you reverse "* “, it becomes " *”. Or another way of looking at it is that:

line += "* ";

is the culprit.


#3

Do you mean for this line: var space = ""; to be var space = " ";?


#4

thanks :slight_smile: now It’s fine


#5

I want just to create an empty variable to use later


#6

but the problem now is that I have an extra space after


#7
function makeLine(length) {
  for (var line="", j=1; j <= length; j++)
    line += j === length ? "*" : "* ";
  return line + "\n";
}

function buildTriangle(length) {
  for (var space="", i=1; i<=length; i++)
    space += makeLine(i);
  return space;
}
console.log(buildTriangle(10));

Output:

*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *

#8

Thank you. It solved the problem but please if it is possible can you explain your code step by step because I am not sure I understand it well


#9

What part do you not understand? If you are referring to the following line:

line += j === length ? "*" : "* ";

I was making use of the ternary operator. That one line could have been written as:

if (j === length)
  line += "*"; // end of line so no space at end
else
  line += "* "; // add space

#10
   for ( var line = "", j=1; j <= length; j++)

this part.why var line is inside for loop?

and second question if you use ternary operator you don’t need } { in loop?


#11

You can declare variables using var inside or outside the loop. They have the same scope. Only when you use let or const to declare variables do they have block scope.

The reason I did not use a { or a } after the for loop is if you only have one line of code which needs to execute inside for loop, then you can avoid using the curly brackets. If you have more than one line which needs to execute inside the for loop, then you must use the { and } to tell JavaScript it is a block of code.


#12

thank you so much for your help and answers :slight_smile:


#13

Without any for loops and branching:


const naturalRange = (min, max) =>
  Array.apply(null, { length: max - min + 1 })
       .map((_, i) => i + min)


const makeLine = symbol => length =>
  symbol + ` ${symbol}`.repeat(length - 1)


const buildTriangle = length =>
  naturalRange(1, length)
    .map(makeLine('*'))
    .join('\n')