+ 3

After some hours of trying, I got duplicated results! I don't know where the problem lies. Please help.

Recursively adding names in deep nested object https://code.sololearn.com/ca10a9A19a11/?ref=app

12th Jul 2021, 11:44 PM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
6 Respostas
+ 2
because you add 'name' property how many times there's keybwich are not array ('name' and 'value')... to get only names, don't iterate keys, just do: function addNames(tree){ return tree.name+tree.children.reduce((a,o) => a+addNames(o),"") } if you are not sure that 'children' contains an array and/or children is defined: function addNames(tree){ return tree.name+(tree.children && Array.isArray(tree.children) ? tree.children.reduce((a,o) => a+addNames(o),"") : "") }
13th Jul 2021, 12:04 AM
visph
visph - avatar
+ 3
visph I'm here again good friend. I got duplicated results. I don't know why. So close
12th Jul 2021, 11:46 PM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 3
visph aha I seee. I get it now! I was so close. Thanks bro ☺️ ☺️ ☺️ ☺️ ☺️
13th Jul 2021, 12:18 AM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 3
visph i think I've noticed a pattern in approaching such problems. First, finding the simplest form of the problem and then writing code to solve that but when there's a difference in the data type or structure, an if statement is used together with the function itself to cater for it. That's what I realized. I'll apply this method to the other questions ☺️☺️☺️. Thanks @visph
13th Jul 2021, 12:24 AM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 3
visph aha cool 😎 😎 😎
13th Jul 2021, 12:32 AM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 1
maybe: I must confess that my mind / brain works usually more well with the intuitive / instinctive approach ;P
13th Jul 2021, 12:31 AM
visph
visph - avatar