Home > OS >  How to make upside-down inverted triangle pattern
How to make upside-down inverted triangle pattern

Time:09-11

I want to make a pattern like in the picture, but the result is different.

enter image description here

Attempt:

Var stars = ';


for (var i = 10; i > 0; i--) {
  for (var j = 0; j < i; j  ) {
    stars  = '*';
  }
  stars  = '\n';
}
console.log(stars);

CodePudding user response:

You need to fill in spaces in the beginning when needed:

let stars = '';

for (let i = 0; i < 10; i  ) {
  for (let j = 0; j < 10; j  ) {
    stars  = (j >= i) ? '*' : ' ';
  }
  stars  = (i === 9) ? '' : '\n';
}

console.log(stars);

CodePudding user response:

var stars = '';
// number of spaces
var k = 2*10 - 2;

// Outer loop to handle number of rows
// n in this case
for (var i = 10; i > 0 ; i--) {
  // Inner loop to handle number spaces
  // values changing acc. to requirement
  for (var j = 0; j < 10-i; j  ) {
    stars  = ' ';
  }
  k = k-2
  // Inner loop to handle number of columns
  // values changing acc. to outer loop
  for(var j = 0; j < i; j  ){
    stars  = '*';
  }
  stars  = "\n";
}
console.log(stars);

  • Related