Skip to content

Instantly share code, notes, and snippets.

@hrishi7
Created March 17, 2021 17:56
Show Gist options
  • Save hrishi7/d7925d63d6910ff4763b9d9b40b224d7 to your computer and use it in GitHub Desktop.
Save hrishi7/d7925d63d6910ff4763b9d9b40b224d7 to your computer and use it in GitHub Desktop.
Wysa LeetCode Challenge Solution
var obj = {
a: {
b: {
c: 12
}
},
findPath :function (str) {
let x = str.split('.');
let ans = null;
for(let i = 0; i<= x.length - 1; i++){
if(ans == null){
ans = this[x[i]];
// console.log(ans)
}else{
ans = ans[x[i]]
}
}
return ans;
}
};
console.log(obj.findPath('a.b.c')); // 12
console.log(obj.findPath('a.b')); // {c: 12}
console.log(obj.findPath('a.b.d')); // undefined
console.log(obj.findPath('a.c')); // undefined
console.log(obj.findPath('a.b.c.d')); // undefined
console.log(obj.findPath('a.b.c.d.e')); // undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment