This is one of those job interview tests on HackerRank that I am embarrassed to say I didn't complete within the allotted 20 minutes, and it's driving me nuts. Basically, you're given the following:
function myList() { // write code here } function main() { // You can't edit this function. This is just pseudo, from what I can remember const obj = myList(); if (operation === "add") obj.add(item); else if (operation === "remove") obj.remove(item); else obj.getList(); // Some more stuff here } So my first train of thought was to write the following inside myList():
let objList = []; function add(item) { objList.add(item) } // etc., etc. return {add, remove, getList}; Needless to say, the above didn't work, with the error: obj.add is not a function, in the main function.
I tried experimenting with this and the returns (only returning add, with or without {}), but didn't really get anywhere. What could I have done to get this working?