I put in a lot of effort to create my own flatten function using functional programming. I have everything working smoothly except for some reason, false is not being included in the result. When I try to use it as an answer on codewars, it works for all scenarios except this one:
flatten([[[1,2,3]]]) // => [[1,2,3]]
Instead, my code is producing:
flatten([[[1,2,3]]]) // => [1,2,3]
What's frustrating is that I believe my output makes more sense than the expected one. However, regardless, I still want to resolve this issue. The problem lies in the fact that the structure I've created is quite complex, making it challenging to make modifications without disrupting everything and needing to refactor the entire codebase. I would like to create a specific function to handle cases like the one above but I'm unsure how to do so without causing disruption to the current setup. Any ideas on how to approach this?
All the other discussions related to this topic seem to focus on numpy, nothing relevant to JavaScript. Help, please!
Here's the code snippet:
var flatten = function (parentArray) {
return reduce(pushArray,[],parentArray);
}
function reduce(combine, base, array) {
forEach(array, function(element) {
base = combine(base,element);
});
return base;
}
function pushArray(baseArray,tempArray) {
while (tempArray[0]) {
baseArray.push(tempArray.shift()); }
return baseArray;
}