JavaScript code for creating a left triangle star pattern

Posted by

Left Triangle Star Pattern in Javascript

Left Triangle Star Pattern in Javascript

In this article, we will create a left triangle star pattern using Javascript. We will use nested loops to achieve this pattern.

// Define the height of the triangle
const height = 5;

// Iterate over each row
for (let i = 0; i < height; i++) {
let row = '';
// Print spaces
for (let j = 0; j < height – i; j++) {
row += ' ';
}
// Print stars
for (let k = 0; k <= i; k++) {
row += '*';
}
// Print the row
console.log(row);
}

body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
text-align: center;
}
p {
text-align: justify;
margin-bottom: 20px;
}