Javascript - .repeat(), .startsWith(), .endsWith()
Code Life
By Pierre Marsaa
Repeating a String
Suppose you want to display a Codémon's health bar using "#" blocks. Building the string with a for loop works, but it takes several lines of code for a very simple task:
↗️
Loading editor...
↶
↷
The .repeat(count) method does the exact same job in a single call: you give it a number, and it returns a new string with the original text repeated that many times:
↗️
Loading editor...
↶
↷
Calling .repeat(0) returns an empty string (""). This is super convenient for health bars: when a Codémon reaches 0 HP, "#".repeat(0) naturally produces an empty bar without needing an if statement!
Verify