I am building an algebra calculator and I'm working on a recursive function to filter like terms from a polynomial. The function below works in that it produces the desired array of arrays of like terms. I can verify this by adding a console.log statement to the function. However, for some reason, the function won't return the output. It returns "undefined".
My thinking is that the chain of recursive calls should terminate with the end condition indicated below, and then pass the returned argument[1] array through the stack.
I've read similar questions on here where the person forgets to put a return statement in one or more places. However, in my code, I have a return statement with the end condition and with the recursive function call. It's probably something simple I'm missing.
var filterLikeTerms = function (terms) { //takes an array of terms, optional second argument is an array of arrays of similar terms
if (!arguments[1]) arguments[1] = []; //Initilizes the second argument if none is given
if (terms.length == 0) return arguments[1]; //End condition
arguments[1].push(terms.filter(term => terms[0].toString() === term.toString())); //Adds similar terms to the 2nd argument array
terms = terms.filter (term => terms[0].toString() !== term.toString()); //shortens the terms array to exclude the like terms filtered above
return filterLikeTerms(terms, arguments[1]); //recursive function call
}