At the North Pole, the elves have simplified their storage system to avoid mistakes.
They now keep the presents in a magical object with limited depth, where each value appears only once.
Santa needs a quick way to know which path of keys he must follow to find a specific present.
Your task is to write a function that, given an object and a value, returns the array of keys that must be traversed to reach that value.
Rules:
Examples:
const workshop = {
storage: {
shelf: {
box1: 'train',
box2: 'switch'
},
box: 'car'
},
gift: 'doll'
}
findGiftPath(workshop, 'train')
// โ ['storage', 'shelf', 'box1']
findGiftPath(workshop, 'switch')
// โ ['storage', 'shelf', 'box2']
findGiftPath(workshop, 'car')
// โ ['storage', 'box']
findGiftPath(workshop, 'doll')
// โ ['gift']
findGiftPath(workshop, 'plane')
// โ []workshop = {
'storage': {
'shelf': {
'box1': 'train',
'box2': 'switch'
},
'box': 'car'
},
'gift': 'doll'
}
find_gift_path(workshop, 'train')
# โ ['storage', 'shelf', 'box1']
find_gift_path(workshop, 'switch')
# โ ['storage', 'shelf', 'box2']
find_gift_path(workshop, 'car')
# โ ['storage', 'box']
find_gift_path(workshop, 'doll')
# โ ['gift']
find_gift_path(workshop, 'plane')
# โ []
