Why array not showing all names? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why array not showing all names?

Recursive function not pushing items to array https://code.sololearn.com/c5A17A8A9a11/?ref=app

11th Jul 2021, 3:26 AM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
13 Answers
+ 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; }
11th Jul 2021, 4:15 AM
visph
visph - avatar
+ 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; }
11th Jul 2021, 5:11 AM
visph
visph - avatar
+ 4
visph & Ipang Thanks guys. I learnt plenty from your answers. 🙏
11th Jul 2021, 11:36 AM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
+ 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 ...
11th Jul 2021, 5:03 AM
Ipang
+ 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=>{});
11th Jul 2021, 5:21 AM
visph
visph - avatar
+ 3
Visph, Thanks for the explanation 🙏
11th Jul 2021, 6:04 AM
Ipang
+ 2
visph please help me out
11th Jul 2021, 3:51 AM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 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 ;)
11th Jul 2021, 4:18 AM
visph
visph - avatar
+ 2
visph thanks soo soo much bro 😀😀😀😀😀😀. You're amazing!!!
11th Jul 2021, 4:36 AM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 2
11th Jul 2021, 3:35 PM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 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.
11th Jul 2021, 3:53 PM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 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
11th Jul 2021, 4:06 PM
visph
visph - avatar
+ 1
visph thank you very much!!! 😊😊😊😊😊
11th Jul 2021, 4:08 PM
جوردن آهو ماولی
جوردن آهو ماولی - avatar