Tell us what’s happening:
Curious as to why is this not being passed . . . any ideas?
Your code so far
var txt = "";
function repeatStringNumTimes(str, num) {
// repeat after me
if(num>0){
for(var i=0;i<num;i++){
txt += str;
}
return txt;
}else{
return "";
}
}
repeatStringNumTimes("abc", 3);
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36
.
Link to the challenge:
https://www.freecodecamp.org/challenges/repeat-a-string-repeat-a-string
function repeatStringNumTimes(str, num) {
// repeat after me
var newStr = '';
for(var i=0; i<num; i++) {
newStr+=str;
}
return newStr;
}
repeatStringNumTimes("abc", 3);
Your variable txt is accumulating the answers from all of your tests. Declare inside your function so you start with a new variable each test. Good luck!
Doh ! - silly mistake, thanks for that
As @alhazen1 said, it’s because of the global variable txt.
If something unpredictable happens to your code, you can open the console on your browser[*] and log the output of a variable, like console.log(txt), whenever you think there is a problem.
[*]
Shortcuts for Chrome: Ctrl+Shift+J on Linux/Windows & Cmd+Opt+J on Mac.
Shortcuts for Firefox: Ctrl+Shift+K on Linux/windows & Cmd+Opt+K on Mac