Repeat a String for Num times

Valentsea
Total
0
Shares



DESCRIPTION:

Repeat a given string str (first argument) for num times (second argument). Return an empty string if num is not a positive number. For the purpose of this challenge, do not use the built-in .repeat() method.



Examples

repeatStringNumTimes("abc", 3)
//should return `abcabcabc`.
Enter fullscreen mode

Exit fullscreen mode



My approach for solving this problem:

  • return “” if the num is negative.
  • loop for num times
  • storge the str value inside result var
  • return the final result



My solution:

function repeatStringNumTimes(str, num) {
  let result = ""

  if(num<0){
    return ""
  }

  for(let i =0;i<num;i++){
    result += str
  }

  return result;
}

repeatStringNumTimes("abc", 3);
Enter fullscreen mode

Exit fullscreen mode

I breath with your support, sometimes I feel discouraged when I don’t get reactions on my posts. so please Like 👍 & share if you can. Thanks for being here!



Follow Muhmmad Awd on

Valentsea

Valentsea

If you have any questions or feedback, please feel free to contact me at
Valentsea

Total
0
Shares
Valentsea

Was ist Javascript?

✘ Jetzt Premium testen: https://programmieren-starten.de/premium-mitgliedschaft-lp1/?utm_source=youtube&utm_medium=social&utm_term=was-ist-javascript&utm_content=link-in-videobeschreibung&utm_campaign=premium-mitgliedschaft In diesem Video erkläre ich dir, was es mit der Scriptsprache Javascript auf…

You May Also Like