Skip to content

Instantly share code, notes, and snippets.

@CharlesRajendran
Last active October 1, 2019 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharlesRajendran/8030139d7a346ac6be6862bd202af4e8 to your computer and use it in GitHub Desktop.
Save CharlesRajendran/8030139d7a346ac6be6862bd202af4e8 to your computer and use it in GitHub Desktop.
Finding Family Member Age with Recursion
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)
@CharlesRajendran
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment