Created
January 21, 2023 21:08
-
-
Save iamqasimali/388ada25d674b6cd339f1f31593b2e7e to your computer and use it in GitHub Desktop.
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
var luke = { | |
name: "luke skywalker", | |
jedi: true, | |
parents: { | |
father: { | |
jedi: true | |
}, | |
mother: { | |
jedi: false | |
} | |
} | |
} | |
var han = { | |
name: "han solo", | |
jedi: false, | |
parents: { | |
father: { | |
jedi: false | |
}, | |
mother: { | |
jedi: false | |
} | |
} | |
} | |
var anakin = { | |
name: "anakin skywalker", | |
jedi: true, | |
parents: { | |
mother: { | |
jedi: false | |
} | |
} | |
} | |
var characters = [luke, han, anakin]; | |
function fatherWasJedi(character) { | |
var path = "parents.father.jedi"; | |
return path.split(".").reduce(function(obj, field) { | |
if (obj) { | |
return obj[field]; | |
} | |
return false; | |
}, character); | |
} | |
characters.forEach(function(character) { | |
console.log(character.name + "'s father was a jedi:", fatherWasJedi(character)) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment