Some THING WRONG?

Tell us what’s happening:
My console.log for some reason adds values to the function?
I tried:
Your code so far


var applePen = '';
function repeatStringNumTimes(str, num) {
 if (num >= 0) {
 for (let i = 0; i < num; i++) {
   applePen += str;
 }
 var penPineapple = applePen
 return penPineapple;
 } 
 return '';
}

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

You can see it repeats it 3 times when only doing console.log()

var applePen = '';
function repeatStringNumTimes(str, num) {
  if (num >= 0) {
  for (let i = 0; i < num; i++) {
    applePen += str;
  }
  var penPinapple = applePen
  return penPinapple;
  } 
  return '';
}

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

The tasks asked for this, but don’t accept it, why?

   **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15.

Challenge: Repeat a String Repeat a String

Link to the challenge:

A) Your variable names are baffling.


B) Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like

YOU ARE RIGHT.

Thanks for the help.

Makes sense.

Didn’t quite understand, how console.log() did that. But now understand that when you use console.log() it add the same (can be different also) values a second time (but it’s fake values). Since I console.log() the function again, it updates it a second time. The test should have actually have worked since I only did it one time. Never said anything about how it did it. Or am I wrong?

Why didn’t it pass it?

My brain tells me it should pass anyways, because it only does it one time?

Please explain this.
I DON’T GET IT!

This has nothing to do with console.log. It has to do with the global variable issue I explained above.

Do you have a question about that explanation?

1 Like

I understand global and local variable now. But when the test only does it once, it should update once. Why does the test hide this?

You can see on my screen shot. I console.log one time it gives the correct answer.

The console.log() doesn’t change the values
It just adds it to the console.
So the test only updates 1 time.

If I do repeatStringNumTimes("*", 1) It does it one time. The test doesn’t ask for multiple times.

The tests are calling repeatStringNumTimes multiple times with different function args each time. Look at the image you pasted above, it is printing an error message for each time it calls the function.

2 Likes

Why is freeCodeCamp doing this?

That’s how you test functions. You call it with different values passed in to make sure it behaves properly each time.

2 Likes

Thanks for explaining.
I have seen it on other websites before, but didn’t expect freeCodeCamp to use the same method, because the challenge didn’t ask for this.

It’s always a good idea to design your functions so that they can be used more than once. Functions that can only be used once tend not to be very helpful.

3 Likes

Will try to remember that for the future.

1 Like