0

I'm very new to coding. What would be the way to execute this using less lines of code?

for (var i=1; i <= 7; i++){
    if (i == 1) console.log("#");
       else if (i == 2) console.log("##");
       else if (i == 3) console.log("###");
       else if (i == 4) console.log("####");
       else if (i == 5) console.log("#####");
       else if (i == 6) console.log("######");
       else if (i == 7) console.log("#######");
}

I'ver been trying to use the while loop but I'm not understanding how to print multiple '#"s in less lines of code

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
FSGeoff
  • 11
  • 2

1 Answers1

1

You can use String#repeat:

for (var i = 1; i <= 7; i++){
    console.log('#'.repeat(i));
}
Unmitigated
  • 76,500
  • 11
  • 62
  • 80