Repeat a String for Num times

xmohammedawad

Mohammed Awad

Posted on May 7, 2023

Repeat a String for Num times

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

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

💖 💪 🙅 🚩
xmohammedawad
Mohammed Awad

Posted on May 7, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related