Created
March 17, 2021 17:56
-
-
Save hrishi7/d7925d63d6910ff4763b9d9b40b224d7 to your computer and use it in GitHub Desktop.
Wysa LeetCode Challenge Solution
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 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