Repeat a string challenge - Tips please

Just tips/pointers (without solutions) please. I have used a while loop, but I am unsure of what to put inside of it. Simply typing in return str doesn’t work, because this will kick me out of the loop. Can anyone explain why str =str + "" isn’t recognized? And, what areas do I need to read up on, look more carefully at, understand better, etc.? Thank you in advance.

function repeatStringNumTimes(str, num) {
  // use a while loop
  i = 0;
  while (i < num) {
    i++;
    str = str + "";
  }
  return "";
}

repeatStringNumTimes("abc", 3);

It is recognizing str = str + ""; but you are telling it to add "" to str, which is nothing, so its literally doing just that, adding nothing to str which results in no change to str.

return ""; is saying to return no value, which is what it does.

Think about what value it is you want added to string…
and for return, think about what value it is you want it to output…

Question: The challenge requires us to return an empty string if num is not a positive number. Does the statement return "" not do this?

Think of it this way… you have a conditional statement… if the string does not pass the conditional statement, it will return nothing anyway. So no need to tell it to return nothing.

What you need is to tell it what to return if it does pass your conditional statement.

in other words:
Its like telling the computer, if you have more than 0 apples, tell me how many apples you have.

If the computer has more than 0 apples, it will tell you how many it does have. If it doesn’t have any apples, it wont say anything.

(trying to help without giving away code like you asked lol hope my examples help!)

1 Like

Got it! Thank you.

function repeatStringNumTimes(str, num) {
  // use a while loop
  strArr = [];
  i = 0;
  while (i < num) {
    i++;
    strArr.push(str);
  }
  return strArr.join('');
}

repeatStringNumTimes("abc", 3);
1 Like

YaY! Im so glad I could help!!!

And so you know, you are the very first person Ive helped with a JS issue, so this makes my day hehehe!!

1 Like

If you want a challenge, try to do it without converting anything into arrays. It can be done :slight_smile:

1 Like

Checking out the answer page now: freeCodeCamp Algorithm Challenge Guide: Repeat a string repeat a string

I have a question about the intermediate code solution:

  if(num < 0)
    return "";
  if(num === 1)
    return str;
  else
    return str + repeatStringNumTimes(str, num - 1);
}

In the explanation, it is stated that if num > 1, 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.

How does JavaScript know to store that whole process as one instance of str? I hope this makes sense.

This is an example of recursion. It can be very confusing at first, but the way to do it is to take a set of small number (1,2,3) and follow through the execution.

So, if num is one, the return value is just str. Expected.

Now, if it is 2, the return value is str + the same function called again. So if we set up a stack of return functions:

return str + repeatStringNumTimes(str, 1) = return str + (return str),

and the point is that the second return will be evaluated before the first one.

1 Like

MDN on recursion and the function call stack

1 Like

This is helpful. I’m looking at an example of recursion provided on https://docs.microsoft.com/en-us/scripting/javascript/advanced/recursion-javascript. In order to factorialize the argument, or number, the stack of return functions would have to look like return str * (return str), and not return str + (return str), correct? Does JavaScript know to multiply each return because it is written in the original return statement return (num * factorial(num - 1))? In other words, the operator in the return statement determines how each subsequent return interacts with the previous return.

function factorial(num)  
{  
    // If the number is less than 0, reject it.  
    if (num < 0) {  
        return -1;  
    }  
    // If the number is 0, its factorial is 1.  
    else if (num == 0) {  
        return 1;  
    }  
    // Otherwise, call this recursive procedure again.  
    else {  
        return (num * factorial(num - 1));  
    }  
}  

var result = factorial(8);  
document.write(result);  

// Output: 40320
1 Like

Yeah, pretty much. Because the function will keep on expanding out any recursive calls, so long as you have some operation that can be chained together on results, and the type of the result you get back from the recursive calls is consistent, everything will work out.

1 Like

My solution:

function repeatStringNumTimes(str, num) {

if (num > 0) {

const repeatedArray = [].concat(...Array(num).fill(str));

const result = repeatedArray.join('');

  return result;

} else {

  return ""

}

}

console.log(repeatStringNumTimes("abc", 3));