Last active
October 1, 2019 01:57
-
-
Save CharlesRajendran/8030139d7a346ac6be6862bd202af4e8 to your computer and use it in GitHub Desktop.
Finding Family Member Age with Recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const profile = { | |
name: "Madasaamy", | |
age: 80, | |
kids: [ | |
{ | |
name: "Soori", | |
age: 57, | |
kids: [ | |
{ | |
name: "Miller", | |
age: 29 | |
}, | |
{ | |
name: "Charles", | |
age: 26 | |
} | |
] | |
} | |
] | |
}; | |
let age = 0; | |
function calculateAge(profiles) { | |
profiles.forEach(e => { | |
age += e.age; | |
if (e.kids) { | |
calculateAge(e.kids) | |
} | |
}); | |
} | |
calculateAge([profile]); | |
console.log(age) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mock Interview: https://www.youtube.com/watch?v=sahZwVK64gk