Question on the challenge "No repeats please"

Tell us what’s happening:
i am pretty new to JS, so I am not sure if I used this language correctly. Based on my previous coding experience, I used dfs method to solve this problem. If i test all the test cases provided, my code will yield the expected result. However I still got “red crosses” next to each test cases. Can some one please tell me what is going on? Or is there something wrong about how I coded. Anything would help. Thx!

Your code so far

var a = [];
function permAlone(str) {
  dfs(str, "", []);
  return a.length;
}
function dfs(str, cur, visited) {
  if (cur.length == str.length) a.push(cur);
  for (var i = 0; i < str.length; i++) {
    if (!visited[i] && str[i] != cur[cur.length - 1]) {
      visited[i] = true;
      dfs(str, cur + str.charAt(i), visited);
      visited[i] = false;
    }
  }
}
permAlone('aaabb');

Link to the challenge:
https://www.freecodecamp.org/challenges/no-repeats-please

Reference error: a is not defined

This is the error message i read when i try to run your code ^^

Hope it helps,
-LyR-

Put the following console.log statement before the first if statement in dfs and run the tests. Check you brower’s console (Ctrl+Shft+J in Chrome) and you will see that the array a is retaining its value between tests.

Why? Because a is a global variable and the FCC tests run consecutively, the array a never gets reset back to a blank array between each test.

Thanks all for the kind help. I have modified it so it now works properly. I did not know I could declare a new function inside a function which is a good thing to know I guess. So now the array a would get reset every time the function perAlone is called back.


function permAlone(str) {
  var a = [];
  dfs(str, "", []);
  function dfs(str, cur, visited) {
  if (cur.length == str.length) a.push(cur);
  for (var i = 0; i < str.length; i++) {
    if (!visited[i] && str[i] != cur[cur.length - 1]) {
      visited[i] = true;
      dfs(str, cur + str.charAt(i), visited);
      visited[i] = false;
    }
  }
}
  return a.length;
}