Advertisement
PortalPlayer

repeatstring

Jun 1st, 2020
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const repeatString = (string, amount) => {
  2.     if (typeof string !== "string") return
  3.  
  4.     if (typeof amount === "string" && !isNaN(parseInt(amount))) {
  5.         let final = ""
  6.  
  7.         for (let i = 0; i < parseInt(amount); i++) {
  8.             final += string
  9.         }
  10.  
  11.         return final
  12.     } else if (typeof amount === "number") {
  13.         let final = ""
  14.  
  15.         for (let i = 0; i < amount; i++) {
  16.             final += string
  17.         }
  18.  
  19.         return final
  20.     } else return
  21. }
  22.  
  23. // example
  24. // code | expected output
  25.  
  26. repeatString("foo", 3)   // foofoofoo
  27. repeatString("foo ", 3)  // foo foo foo
  28. repeatString(" foo", 3)  //  foo foo foo
  29. repeatString(" foo ", 3) //  foo  foo  foo
  30.                          //
  31. repeatString("bar", 3)   // barbarbat
  32. repeatString("bar ", 3)  // bar bar bar
  33. repeatString(" bar", 3)  //  bar bar bar
  34. repeatString(" bar ", 3) //  bar  bar  bar
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement