+ 2
Why array not showing all names?
Recursive function not pushing items to array https://code.sololearn.com/c5A17A8A9a11/?ref=app
13 odpowiedzi
+ 5
sorry, I was busy ^^
your recursive function must first initialize the array as an optional argument: at first call you can pass only the tree, but at inner call, you must past the array as second element to always update the same array ;)
secondly, push the tree.name first, and only call the function recursively for each children (with each child as first tree argument, as you do):
function getChildren(tree,childrenResults=[]){
if (tree.name)
childrenResults.push(tree.name);
if(tree.children.length==0) return;
tree.children.forEach(el=>{
// console.log(el.name)
getChildren(el,childrenResults)
})
return childrenResults;
}
+ 4
Ipang well, you're almost right... I didn't notice that when correcting OP code...
almost, because the return statement at the end is only for convenience: if you provide the second argument at first call, it would not be affected as inner call doesn't rely on return value ;)
so the only downside (wich can easily be fixed by returning array if no children) is if tree has a name value and no children, and you rely on second default argument ^^
better code should be:
function getChildren(tree,childrenResults=[]){
if (tree.name)
childrenResults.push(tree.name);
if(tree.children.length) tree.children.forEach(el=>{
// console.log(el.name)
getChildren(el,childrenResults)
})
return childrenResults;
}
+ 3
Visph,
Awesome solution 👍
I just have one doubt, that the recursive function just return when a branch has no child (tree.children.length == 0).
I thought returning something was necessary for a recursive function. But here it just issue a `return;` statement.
I'm a bit lost here ...
+ 3
better also to test first if tree.children is an array, to avoid error if no length nor forEach property:
if (Array.isArray(tree.children) && tree.children.length) tree.children.length.forEach(el=>{});
+ 3
Visph,
Thanks for the explanation 🙏
+ 2
visph please help me out
+ 2
you could also avoid the second argument by updating (concatenating / pushing each elements) the array with the returned array from inner call, but it's slightly less efficient ;)
+ 2
visph thanks soo soo much bro 😀😀😀😀😀😀. You're amazing!!!
+ 2
👑 Tchybooxuur! Nice 😊
+ 2
visph bro please can you spare me some minutes of your time in private chat? I want some advice on how to upgrade my programming skills. I do tackle projects and I learn from them but I don't learn completely new techniques, just the old ways of solving different problems. I feel like I'm not moving forward. Please some advice or books recommendations.
+ 1
(JAM) جوردن آهو ماولی I do not have chat/messenger feature with the old version of sololearn app I'm using, and I do not want to upgrade ;P
also, I'm not the best suited person to ask for such advices: I've learnt programming by myself by using a lot internet... so I don't have necessarly best practices to code / solve problems, nor adives / book recommendations, except the "You don't know JS" free book series by Kyle Simpson (alias getify), available at:
https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/README.md
+ 1
visph thank you very much!!! 😊😊😊😊😊