Home > Blockchain >  Why is my JS recursion function returning a list populated with the same entry?
Why is my JS recursion function returning a list populated with the same entry?

Time:06-18

I've been learning Python at School, and I am learning Javascript on my own time and tackling some JS projects. I can't figure out why my recursion function is only a list with the same entry. I am at my wit's end. Any insight will be greatly appreciated!

Function description: The function takes in a list of course Objects, with key-value pairs "courseCode": string and "possibleCombos": list[number]. I want my recursive function to output another list of Objects, with the course Object's "courseCode" value as its keys, and one element of the "possibleCombos" as its value. The returned list will have all the possible permutations of the Objects with course-combo pairs. The function also takes in an Object parameter, for recursion purposes.

Example data:

const dummyObject1 = {
    'courseCode': 'BLUE',
    'possibleCombos': [1, 2, 3, 4, 5]
}

const dummyObject2 = {
    'courseCode': 'RED',
    'possibleCombos': [11, 22, 33, 44]
}

const dummyObject3 = {
    'courseCode': 'PURPLE',
    'possibleCombos': [111, 222, 333, 444, 555, 666]
}

const dummyList = [dummyObject1, dummyObject2, dummyObject3]``` 

I ideally want:

let dummySchedules = recursionFunction(dummyList, {})
console.log(dummySchedules)

//ideal console output
[
{'BLUE': 1, 'RED': 11, 'PURPLE': 111},
{'BLUE': 1, 'RED': 11, 'PURPLE': 222},
{'BLUE': 1, 'RED': 11, 'PURPLE': 333},
... //and so on.
]

However, the list output I get, is just 120 entries of the same Object.

Here is my code:

function recursiveFunction(listOfCourses, dictSoFar) {
    //base case, checks if listOfCourses is empty
    if (!listOfCourses.length) {
        return [dictSoFar]
    } else {
        //recursive step

        var arraySoFar = [] //accumulator

        //iterate through each element of listOfCourses[0]['possibleCombos']
        for (let combo of listOfCourses[0]['possibleCombos']) {
            //update dictSoFar entry.
            dictSoFar[listOfCourses[0]['courseCode']] = combo
            
            //filter out the course we just entered into dictSoFar.
            let course = listOfCourses[0]
            var cloneListOfCourses = listOfCourses.filter(item => item !== course)
            
            //recursive call, this time with the filtered out list. If we keep following the
            //the recursive call down, it should reach the point where listOfCourses is empty,
            //triggering the base case. At that point, dictSoFar already has all course: combo 
            //pairs. This should traverse through all possible course: combo pairs.
            var result = recursiveFunction(cloneListOfCourses, dictSoFar)
            
            //update the accumulator
            arraySoFar.push(...result)
        }
        return arraySoFar;
    }
}

What is happening? On theory I think the logic makes sense, and I can't tell where its going wrong.

CodePudding user response:

you can do something like this

if you need some explanation fell free to ask

const dummyObject1 = {
  'courseCode': 'BLUE',
  'possibleCombos': [1, 2, 3, 4, 5]
}

const dummyObject2 = {
  'courseCode': 'RED',
  'possibleCombos': [11, 22, 33, 44]
}

const dummyObject3 = {
  'courseCode': 'PURPLE',
  'possibleCombos': [111, 222, 333, 444, 555, 666]
}

const dummyList = [dummyObject1, dummyObject2, dummyObject3]

function recursiveFunction(listOfCourses) {

  const loop = (data, acc) => {
    
    if (!data.length) { // if listOfCourses is falsy
      return acc
    }
    const [next, ...rest] = data
    
    if(acc.length === 0){
      return loop(rest, next)
    }
    
    return loop(rest, next.flatMap(n => acc.flatMap(a => Object.assign({}, a, n))))
  }
  const courseCombo = listOfCourses.map(({
    courseCode,
    possibleCombos
  }) => possibleCombos.map(c => ({
    [courseCode]: c
  })))
  return loop(courseCombo, [])

}

console.log(recursiveFunction(dummyList))

  • Related