Issue
I have been asked this question in an interview. How to solve this? The object and consoles statements are mentioned. I am not getting how to implement the function findPath?
Solution
class Obj {
constructor() {
this.data = {
a: {
b: {
c: 12
}
}
};
}
findPath = (str) => {
let sol = this.data;
for (let key of str.split(".")) {
sol = sol[key];
if (!sol) {
return undefined;
}
}
return JSON.stringify(sol);
};
}
let obj = new Obj();
console.log(obj.findPath("a.b.c"));
console.log(obj.findPath("a.b"));
console.log(obj.findPath("a.b.d"));
console.log(obj.findPath("a.c"));
console.log(obj.findPath("a.b.c.d"));
console.log(obj.findPath("a.b.c.d.e"));
Answered By - Ketan Ramteke Answer Checked By - Candace Johnson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.