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
  • 46,070
  • 7
  • 44
  • 60
FSGeoff
  • 1
  • 1

1 Answers1

1

You can use String#repeat:

for (var i = 1; i <= 7; i++){
    console.log('#'.repeat(i));
}
Unmitigated
  • 46,070
  • 7
  • 44
  • 60