Repeat a string repeat a string few questions

Tell us what’s happening:
1.Declaring str1 globally gives me the required output for all test cases but none of them are satisfied. Why?
2.Declaring str1 just before the if statement(var str1;) gives output undefinedabc even though when I return typeof(str1), it outputs string, then why this issue? undefined goes away when I do var str1 = " ";
But I’d like to know why this happens?

Also I have solved it using while loop but came across a method using if statement and recursion but couldn’t understand it.

Your code so far

var str1 = "";
function repeatStringNumTimes(str, num) {
  // repeat after me

 //  var str1 = " ";
//   while (num > 0) {
//     str1 += str;
//     num--;
//   }
//   return str1;
  if (num > 0) {
    str1 += str;
    repeatStringNumTimes(str, num-1);
  return str1;

repeatStringNumTimes("abc", 3);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36.

Link to the challenge:

Initializing the variable even with empty quotes will make it a string, so giving no initialization makes it undefined.

var x = []; // type is object (arrays are objects)
var x = ''; // type is string
var x = 0; // type is number
var x = NaN; // type is number (not a numbers are numbers)
var x; // type is undefined

The if statement below will only run once, so if you are expected to repeat the string three times you would only get the original string in this case unless you init str1 as:

str1 = str;

I’m not totally sure how that statement would work since it expects to run the full function inside the if statement as well, but without a loop it seems it would still only run once. I imagine the result would be : “abc”.

So I’m guessing return typeof(str1) outputs string because of str1 += str as it runs once. Is that it?
And any idea about my first question? (Point no 1)

Because when the FCC tests run, anything variables declared globally (outside your function) will the values they had when the last test finished.

For demonstration purposes, if we use your current code as written (with the global str1), and make the following 3 calls to your function, you can see str1 contains the value of the last call to the function in each case.

console.log(repeatStringNumTimes("*", 3)); 
// results in '***'

console.log(repeatStringNumTimes("abc", 3));
// results in '***abcabcabc' 

console.log(repeatStringNumTimes("123", 4)); 
// results in '***abcabcabc123123123123'

Oh yes. That makes sense. You told me this in a previous post. But then it should show those previous function call results concatenated to the latest function call result in the FCC console as well right? It doesn’t show that.

The FCC console is limited in what it shows. It is best to use the browser’s console (Ctrl+Shft+I in chrome). You need to become familiar with using the browser’s console and other development tools as you will definitely need them for more complicated challenges and the projects.

Yes. Tried it in the browser and it worked just as you had mentioned. Thank you.