rafon2000
rafon2000

Reputation: 3

JS simple christmas tree on one loop

I'm trying to make one loop Christmas tree using string.repeat method.

let Height = 6;
let Star = "*";

for (let i = 0; i < Height; i++) {
  for (let j = 0; j <= i; j++) {
    document.write(Star);
  }
  document.write("<br>");
}

I've created it on 2 loops, but don't know how to do it on a single one. Thank you for help.

Upvotes: 0

Views: 316

Answers (1)

BenM
BenM

Reputation: 53238

You can use the repeat() method of a string:

const Height = 6;
const Star = "*";

for (let i = 0; i < Height; i++) {
  document.write(`${Star.repeat(i + 1)}<br />`);
}

Upvotes: 1

Related Questions