Build a Permutation Generator - Build a Permutation Generator

Tell us what’s happening:

i fall in infinite loop, 1 - how can i determine the base case to stop this function call.
2 - am i getting the prefix right ?
thanks

Your code so far

// [1] create a permuteString function with 3 params (str, prefix, results)
const permuteString = (str, prefix, results) => {

  // prefix = removed char to make permutations
  // if the length is 0 push prefix and return results
  // iterate(forEach) over str remove current char from the str and call permuteString function() recursively with updated arguments 
  // return final results []
  // permutations must be unique (new Set() will be benifit for it)
  results = []

  //   str.length - 1
  //   results.push(str[str.length])
  //   permuteString(str.length - 1)
    // arr.push(prefix)

     str.split('').forEach((item, idx) => {
       let splitted = str.split('')
       prefix = splitted.splice(idx, 1)
       prefix = prefix.join()
      if(str.length == 0) {
      results.push(prefix)
      return

  }
      let remainingStr = splitted.splice(idx).join('')
       console.log(remainingStr)
      //  console.log()
      })
      // prefix = str.slice(i, 1)
      permuteString(str, prefix, results)
    
  
  return [... new set(results)]
}

permuteString("far")

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36

Challenge Information:

Build a Permutation Generator - Build a Permutation Generator

User Story 3 sounds like your base case:

  1. Inside the function, you should check if the length of the passed string is 0. If it is, push the current prefix to the results and return the results.

Are you doing this?:

remove the current character from the string

1 Like

Before we start building the recursive portion of our function, we need to establish our base case first.

It looks like User Story #3 explains your base case.

What happens to your results array every time the function is called recursively? Can you think of another way you can set the initial value of results to an empty array?

1 Like

yes i know the default parameter on the declaration of the function

what a stupid was i !! :slight_smile:
thanks for your help

so i need to push prefix to results array
removing first letter from the string
then prefix will be the string after removed character
etcetera …

is that the right steps i shall go with ?

you have a good start, but…

always the first letter?

um, no, maybe the second letter i see in the tests

now i feel comfortable after i saw some reviews about the challenge from the moderators and how hard it is,
so i will delay it for a while, and i will continue my learning path,
not to get despair so early,
i feel that iam not ready yet
thanks for your help

here is my updated code

// [1] create a function named permuteString
// [2] permuteString function should take three parameters:(a string, a prefix value and an empty array)
function permuteString(str, prefix = '', results = []) {
  // [3] check if the length of the passed string is 0
  if(str.length === 0) {
    results.push(prefix)
    // return results
  }
  let splitted = str.split('')
  splitted.forEach((item, idx) => {
    let current = str.indexOf(item)
    prefix = str.slice(idx, 1)

    let remaining = str.slice(idx)
    results.push(prefix + remaining)
    // str = str.slice(idx)
    console.log(str.indexOf(item))
    // permuteString(str, prefix, results)
  })
  return results
}
console.log(permuteString("far")) 

This challenge was tougher than I thought at first glance. Please do not forget the structure if {} else {}. I would suggest using replace() and updating the prefix when passing the parameters into the recursive function, so you can keep the previous prefix and string unchanged, so that your for… loop is not modified owing to a shorter string and longer prefix.

And keep in mind that you need unique values in your Result array.

1 Like

special thanks to you, i may delay it to the that iam free in weekend,
i feel my mind stop working when i look at this challenge,
i know this is pure feeling, i will pass it by time