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


This content originally appeared on DEV Community and was authored by Muhmmad Awd

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`.

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);

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


This content originally appeared on DEV Community and was authored by Muhmmad Awd


Print Share Comment Cite Upload Translate Updates
APA

Muhmmad Awd | Sciencx (2023-05-07T05:58:00+00:00) Repeat a String for Num times. Retrieved from https://www.scien.cx/2023/05/07/repeat-a-string-for-num-times/

MLA
" » Repeat a String for Num times." Muhmmad Awd | Sciencx - Sunday May 7, 2023, https://www.scien.cx/2023/05/07/repeat-a-string-for-num-times/
HARVARD
Muhmmad Awd | Sciencx Sunday May 7, 2023 » Repeat a String for Num times., viewed ,<https://www.scien.cx/2023/05/07/repeat-a-string-for-num-times/>
VANCOUVER
Muhmmad Awd | Sciencx - » Repeat a String for Num times. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/05/07/repeat-a-string-for-num-times/
CHICAGO
" » Repeat a String for Num times." Muhmmad Awd | Sciencx - Accessed . https://www.scien.cx/2023/05/07/repeat-a-string-for-num-times/
IEEE
" » Repeat a String for Num times." Muhmmad Awd | Sciencx [Online]. Available: https://www.scien.cx/2023/05/07/repeat-a-string-for-num-times/. [Accessed: ]
rf:citation
» Repeat a String for Num times | Muhmmad Awd | Sciencx | https://www.scien.cx/2023/05/07/repeat-a-string-for-num-times/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.