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
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?
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.
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.