No repeats challenge returns the correct answer , but it doesn't work

No repeats challenge returns the correct answer , but it doesn't work
0

#1

Hi , im doing the No repeats challenge. The function that i created returns the right answers , but it is not accepted:

I posted the code in codepen:Code pen Norepeatchallenge

No repeat challenge


#2

Chances are it’s because you’re declaring a global variable.

Try to call your permAlone() function at the bottom of the page more than once and check the results. You’ll see what’s wrong and why you shouldn’t use global variables…


#3

ohh…thanks! Do you know how can i fix it??


#4

Recent conversations about global variables here and here.


#5

Thank you!!! i resolved it!!


#6

Congratulations! :sparkles:


#7

I am having the same problem, my code is here: https://repl.it/@DavidVendel/No-repeats-please-javascript
but doesn’t work on FCC.
I know I am using a global variabile, array where I am saving my solutions. But how do I do recursive function while saving into an array that I want to use after all the recursion is finished? I don’t know.
Can someone teach me?


#8

As an example, this reverses a string:

function reverse(inputString, outputString = '') {
  if (inputString.length === 0) return outputString;
  return reverse(str.slice(1), str[0] + outputString);
}

JavaScript allows you to use default arguments, in this example outputString = ''.

This means that when you call the function for the first time, you don’t need to pass the empty string that will be used to build the output (in your case you would pass the two arrays you’re using)

reverse('Hello')

In the example, the function checks if the string is ''. On the first 5 iterations it isn’t, and when it isn’t, it calls reverse again, but this time, it passes the rest of the string as the first argument, and concatenates the first letter to the output:

reverse('Hello')
// which will be evaluated as
reverse('Hello', '')
// now recurse
reverse('ello', 'H')
reverse('llo', 'eH')
reverse('lo', 'leH')
reverse('o', 'lleH')
reverse('', 'olleH')
// inputString is length 0, return 'olleH'

(note recursion is not optimised in JS, and a recursive solution to the permutations challenge, which can need a very large number of calculations, is likely to be seriously suboptimal)