The program is very simple, we have to take a variable and return that variable being repeated certain amount of times. No need to add space or anything, just keep repeating it into one single string.
The program is very simple, we have to take a variable and return that variable being repeated certain amount of times. No need to add space or anything, just keep repeating it into one single string.
Hint 2
You can’t edit strings, you will need to create a variable to store the new string.
Hint 3
Create a loop to repeat the code as many times as needed.
Hint 4
Make the variable created store the current value and append the word to it.
Solutions
Solution 1 (Click to Show/Hide)
function repeatStringNumTimes(str, num) {
let accumulatedStr = "";
for (let i = 0; i < num; i++) {
accumulatedStr += str;
}
return accumulatedStr;
}
Code Explanation
Create an empty string variable to store the repeated word.
Use a for loop or for loop to repeat code as many times as needed according to num
Then we add the string to the variable created on step one inside of the loop.
At the end of the loop, return the variable for the repeated word.
function repeatStringNumTimes(str, num) {
if (num < 1) {
return "";
} else {
return str + repeatStringNumTimes(str, num - 1);
}
}
Code Explanation
This solution uses recursion.
We check if num is negative and return an empty string if true.
If not, we add the string to a call of our function with num being decreased by 1, which will add another str and another… until eventually num is 1. And return that whole process.
function repeatStringNumTimes(str, num) {
// repeat after me
var repeat="";
for (var i = 1; i<=num ; i++){
repeat = repeat.concat(str);
}
return repeat;
}
repeatStringNumTimes("myName", 4 );
is there any problem in this way of approach for solving this problem
The advanced method here uses the repeat method…which is the best way but the challenge said not to for some reason…to make us work i guess.
i came up with.
function repeatStringNumTimes(str, num) {
return (num<1) ? "": str.concat(repeatStringNumTimes(str,num-1));}
its just a spruced up version of the intermediate code. it uses concat instead of a plus sign and ternary operators. done this way its not much longer than using repeat.
I noticed that a few people posted solutions that are VERY SIMILAR to the FCC solution, but using for loops with decalred var i rather than while loops incrementing num itself. @TatheerHussain@irakligeek
I ran some performance tests, and I found that using a for loop instead of a while loop takes almost TWICE as long! question is: why? is it because you are declaring a second variable in the for loop? is it that much faster just to use num instead of introducing i as well? or is there something about the difference between the two loops that I don’t understand?
1,000,000 repetitions of each function
with for loop and var i: 64.7 milliseconds
with while loop and incrementing num: 34.2 milliseconds
10,000,000 repetitions of each function
with for loop and var i: 562.6 milliseconds
with while loop and incrementing num: 313.6 milliseconds
100,000,000 repetitions of each function
with for loop and var i: 5.5 seconds
with while loop and incrementing num: 3.09 seconds
1,000,000,000 repetitions of each function
with for loop and var i: 54.8 seconds
with while loop and incrementing num: 30.7 seconds
function repeatStringNumTimes(str, num) {
// repeat after me
var more = ‘’;
for(var i = 0; i < num; i++){
if(num > 0){
more += str;
}else{
return ‘’;
}
}
return more;
}
Basicly your top solution doesn’t work because the repeat function only allows values >= 0.
If num is negative it evaluates to an error : Uncaught RangeError: Invalid count value , and therefor JavaScript
wont progress.
So basicly the 2nd solution is the correct way, but you could have solved it that way too:
// store the function in re_peat with (str,num) parameters and return the result of str.repeat(num)
var re_peat = function (str, num) {
return str.repeat(num);
};
// still check if num > 0 to call the function with the Parameters, because see reason above.
if (num > 0)
{
return re_peat(str,num);
}
else
{
return “”;
}