Imagine I have this function:
const createMenu = () => {
const obj = {
consumption: [],
};
return obj;
};
It's a function that, when executed, returns the object
{ consumption: [] }
My goal is to add a key inside that object which is a function. When this function is called with a string parameter, it should push the string into the array inside the 'consumption' key;
This is what I have tried:
const createMenu = () => {
const obj = {
consumption: [],
};
let order = (item) => {obj.consumption.push(item); };
obj.order = order;
return obj;
};
When calling this function within the object with a string parameter, like this:
createMenu().order('pizza');
and then running:
console.log(createMenu().consumption);
I expect the result to be:
['pizza']
However, it doesn't seem to be working as expected. Any help on this would be greatly appreciated.
const createMenu = () => {
const obj = {
consumption: [],
};
let order = (item) => {
obj.consumption.push(item);
};
obj.order = order;
return obj;
};
createMenu().order('pizza');
console.log(createMenu().consumption);