Build a Permutation Generator - Build a Permutation Generator

Tell us what’s happening:

Hi,
I’m failing tests 3 to 8 despite my function returning the expected value for all cases (including the empty string). The only difference I see is that elements in my array have single quotes as compared to the double quotes in the examples. I doubt if that could be the cause of the problem.

Your code so far

function permuteString(str, pref, result) {
  if (str.length === 0){
    result.push(pref)
    return result
  } else {
    for (let j=0; j< str.length; j++){
      permuteString(str.slice(0,j)+str.slice(j+1), pref+str[j], result);
    }
  }
  return Array.from(new Set(result))
}

console.log(permuteString("far","",[]))

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.2 Safari/605.1.15

Challenge Information:

Build a Permutation Generator - Build a Permutation Generator

look at the test cases

permuteString("far") should return [ "far", "fra", "afr", "arf", "rfa", "raf" ] .

what does your function do when called with one single argument like permuteString("far")?

I run into the error:
TypeError: undefined is not an object (evaluating ‘result.push’)
Makes sense since my function tries to access the result argument which I haven’t passed in

I gave the result and pref parameters some default values and all the tests passed. Thanks?:joy:

good job!  

1 Like