When i execute the code output appeared left triangle but i need right triangle

function rtianglePattern() {
let n = 5;
let string = ‘’;
for (let i = 1; i <= n; i++) {
for (let j = 0; j < n - i; j++) {
string += ’ ';
}
for (let k = 0; k < i; k++) {
string += “*”;
}
string += ‘
’;
}
document.getElementById(‘demo3’).innerHTML = string;
}

i need html document only.

Use this logic

// Define the number of rows for the pattern
const numRows = 5;

// Loop through each row
for (let i = 0; i < numRows; i++) {
  let row = '';
  
  // Add spaces to align the stars to the right
  for (let j = 0; j < numRows - i - 1; j++) {
    row += ' ';
  }
  
  // Add stars to the row
  for (let k = 0; k <= i; k++) {
    row += '*';
  }
  
  // Print the row
   console.log(row)
}

but once execute the code it will show not exactly againg run the code some times correctly. How?
right triangle
but i need the HTML document.