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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.