JavaScript program to print Reverse Pyramid with stars


Following program shows you how to print reverse pyramid with stars using for loop

let n = 5; // you can take input from prompt or change the value
let string = "";
// External loop
for (let i = 0; i < n; i++) {
  // printing spaces
  for (let j = 0; j < i; j++) {
    string += " ";
  }
  // printing star
  for (let k = 0; k < 2 * (n-i) - 1; k++) {
    string += "*";
  }
  string += "\n";
}
console.log(string);

Output:

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

Try it online here https://onecompiler.com/javascript/3x5qvcfnd