Q1:
Given the following function works, what are the problems that you can identify?
How would you improve on said problems?
const items = [
{ type: ‘phone’, color:’gold’, device: ‘Nokia’},
{ type: ‘phone’, color:’bronze’, device: iPhone},
{ type: tv, color:’brown’, device: Sony}
]
const excludes = [
{k: ‘color’, v:’gold’},
{k: ‘type’, v:’tv’}
]
function applyFilter(items, excludes) {
excludes.forEach(pair => {
items = items.filter(item => item[pair.k] !== pair.v)
}
return items
}
---
Q2:
How would you construct the Emitter object?
Emitter emitter = new Emitter();
Const sub = emitter.subscribe(‘event_name’, callback)
Const sub2 = emitter.subscribe(‘event_name2’, callback2)
Const sub3 =emitter.subscribe(‘event_name3’, callback)
emitter.emit(‘event_name’, ‘foo’, ‘bar’, ‘baz’);
sub.release()
sub2.release()
sub3.release()