Airbnb Interview Question

Given: var thing = new Thing(); How would you implement the following functionality: thing.set('x', val); thing.set('y', val2); console.log(thing.get('x')); // val console.log(thing.get('y')); // val2

Interview Answers

Anonymous

Apr 20, 2016

Poorly.

3

Anonymous

Feb 23, 2017

function Thing() { let list = []; this.set = function(key, value) { let collection = { key: key, value: value }; list.push(collection); }; this.get = function(key) { return list.find(function(coll) { return coll.key === key; }).value; }; }