Repeat a string repeat a string

Don’t be discouraged! You aren’t alone, there are so many of us in the same boat with you. At those difficult times where if feel like banging my head against the keyboard and burning every computer I can get my hands on, I try to remember that these challenges were designed to force us to search for answers in order to practice problem solving. Besides, whats the point of this online community if everyone is solving everything on their own instead of needing each other?!? The more headaches you’re willing to go through (that other people aren’t) just means less competition when you finally start showing off your hard-earned skills for the big bucks! You got this! Just keep showing up!!! Any and all other applicable cliches!!!

It’s funny, I have been using repl.it to test my JS code. The processes using recursion, ES6 or array joins work in repl.it. But, processes using a While (@P1xt example ), or For loops don’t work.

For FCC, it’s reverse, recursion and ES6 don’t work. But, While and For loops do work.

Sometimes, I feel I am coding against FCC tests instead of the best JS coding methods.

[EDIT]

I must be doing something wrong, now the recursion and ES6 repeat work in FCC.

At work, I use Firefox, they cripple Chrome too much,
At home, I use Chrome.

function repeatStringNumTimes(str, num) {
  if(num>=1)
    return str.repeat(num);
  else
    return "";
}

repeatStringNumTimes("abc", 3);

I actually got it to work, with very minimal googling about concat(). I’m happy and proud of it :), because every challenge in this section confuses me.

function repeatStringNumTimes(str, num) {
  var final = "";
  for(var i = 0; i < num; i++){
    final = final.concat(str);
  }
  return final;
}

repeatStringNumTimes("abc", 3);

Here is what I was able to come up with:

function repeatStringNumTimes(str, num) {
// repeat after me
var repStr = “”;
for (var i = 0; i < num; i++) {
repStr += str;
}

return repStr;
}

repeatStringNumTimes(“abc”, 3);

not sure what the

result.shift()

is for?

thanks

.shift() removes the first element of the array, and returns that first element. The remaining elements in the array are shifted to fill in the gap left by removing the first element.

var array = [22, 33, 44, 55, 66];
var num = array.shift();

// num is now 22. array is now [33, 44, 55, 66]

What is the reason? I was stuck on this exercise as well, my function was returning what it should but I couldn’t pass the challenge, in the end I copy/pasted a code that looked similar to mine to see if it worked and if yes, compare it, this is how I found out. But I would like to know why I’m doing this :slight_smile:

Thanks!

The tests will reuse global variables, so results from a test will propagate to the next one.

You can see this by console.logging the global variable and inspecting your browser’s console. This is what you’ll see when there are globals:

image

This is what you should see (the same as the tests’ expected values):

image

2 Likes

Tell us what’s happening:

Your code so far

var newStr = '';
function repeatStringNumTimes(str, num) {
for (x=0; x<num; x++){
  newStr +=str;
  }
  return newStr;
}

repeatStringNumTimes("abc", 3);

So this is what I had (I corrected it since, putting my global variable inside the function). Individually the tests passed but when tested one after another I had the problem that @kevcomedia mentioned. Thank you for the clear explanation!