No repeats please - code works on jsfiddle, but not on freeCodeCamp

Tell us what’s happening:

My code is working on jsfiddle, passing all the tests, here:

But it’s not working on freeCodeCamp.

Anyone have any idea why?

Your code so far

//helper method (got this from StackOverflow)
function hasDuplicates(str) {
  var duplicates = (/([a-z])\1/i).test(str)
  return duplicates
//console.log( hasDuplicates('aba') )

function getPermutations(string) {
  if (string.length < 2) {
  	console.log('string.length is less than 2... return string!');
		return string; // This is our break condition

  var permutations = []; // This array will hold our permutations

  for (var i = 0; i < string.length; i++) {
    var character = string[i];

    var current = string.slice(0, i) //adding this
    //console.log('current is: ', current)

    var next = string.slice(i + 1, string.length) //adding this
    //console.log('next is: ', next)

    var remainingString = current + next;
    console.log('remainingString is: ', remainingString)
      //refactoring below line into above:
      //var remainingString = string.slice(0,i) + string.slice(i+1,string.length); //Note: you can concat Strings via '+' in JS

    for (var subPermutation of getPermutations(remainingString)) //calling recursively
      var result = character + subPermutation
      console.log(result, ' gets pushed to permutations')
  console.log('permutations is: ', permutations)
  console.log('permutations.length is: ', permutations.length)
  return permutations;

//var array = getPermutations('aab'); //this is where we call above

//final function, calls functions above:
function permAlone(string) {
	if (string.length === 1) {
  console.log('string.length is 1')
  return 1
  //var array = getPermutations('aab'); //this is where we call above
  var array = getPermutations(string); //this is where we call above
  console.log('array from getPermutations(string) is: ', array)

  var answer = {
    if (hasDuplicates(string) === false) {
    	console.log('removing strings with duplicates')
      return string
  answer = answer.filter(n => n) //filter out null values
  console.log('_______answer is: ', answer)
  console.log('_______answer.length is: ', answer.length)
  return answer.length
permAlone("aab") //should return 2.
//permAlone("aaa") //should return 0.
//permAlone("aabb") //should return 8.
//permAlone("abcdefa") //should return 3600.
//permAlone("abfdefa") //should return 2640.
//permAlone("zzzzzzzz") //should return 0.
//permAlone("a") //should return 1.

//permAlone("aaab") //should return 0.
//permAlone("aaabb") //should return 12.

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36.

Link to the challenge:

You seem to have forgotten braces at this part.

for (var ... of ...) {
  var result = ...;

Then remove the console.log()s because they’ll slow down your code.

You might also consider putting semicolons where there are supposed to be semicolons. Before I found the missing braces I noticed a test case going green when I added them.

1 Like

Amazing! That did it.

You’re right, the missing braces were causing the tests not to pass, and too many console.log()s were really slowing down the code.

Thanks so much!