Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const repeatString = (string, amount) => {
- if (typeof string !== "string") return
- if (typeof amount === "string" && !isNaN(parseInt(amount))) {
- let final = ""
- for (let i = 0; i < parseInt(amount); i++) {
- final += string
- }
- return final
- } else if (typeof amount === "number") {
- let final = ""
- for (let i = 0; i < amount; i++) {
- final += string
- }
- return final
- } else return
- }
- // example
- // code | expected output
- repeatString("foo", 3) // foofoofoo
- repeatString("foo ", 3) // foo foo foo
- repeatString(" foo", 3) // foo foo foo
- repeatString(" foo ", 3) // foo foo foo
- //
- repeatString("bar", 3) // barbarbat
- repeatString("bar ", 3) // bar bar bar
- repeatString(" bar", 3) // bar bar bar
- repeatString(" bar ", 3) // bar bar bar
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement