Build A Node.js module called inventory.js that exposes the following functional
ID: 3737164 • Letter: B
Question
Build A Node.js module called inventory.js that exposes the following functionality:
addItem - A function that takes 2 parameters: an item’s name and price and tracks it internally.
items - A function that returns a list of items, sorted by price from least expensive to most expensive.
getMostExpensive - Returns an object that contains the item’s name and price for the most expensive item that has been added.
getLeastExpensive - Returns an object that contains the item’s name and price for the most least expensive item that has been added.
removeItem - Remove an item by name.
getItemByName - Returns an item’s name and price by name.
getItemByPrice - Returns an item’s name and price by price.
Your module should not expose any functions or variables that would allow the above functions to have the integrity of their data compromised.
BELOW IS AN EXAMPLE OF HOW THE SCRIPT WILL BE USED.
'use strict';
const inventory = require('./inventory.js');
inventory.addItem('orange juice', 2.40);
inventory.addItem('milk', 4.00);
inventory.addItem('eggs', 1.20);
inventory.items().forEach(function (item) {
console.log(`Item ${item.name} costs ${item.price}`);
});
inventory.removeItem('milk');
let mostExpensive = inventory.getMostExpensive();
console.log(`${mostExpensive.name} at ${mostExpensive.price} costs the most!`);
let leastExpensive = inventory.getLeastExpensive();
console.log(`${leastExpensive.name} at ${leastExpensive.price} costs the least!`);
let itemByName = inventory.getItemByName('eggs');
console.log(`${itemByName.name} with price of ${itemByName.price} retrieved by name`);
let itemByPrice = inventory.getItemByPrice(2.40);
console.log(`${itemByPrice.name} with price of ${itemByPrice.price} retrieved by price`);
Explanation / Answer
Here is your node module. Functions do as the name suggest.
inventory.js
(function() {
function Inventory() {
this.item_list=[];
};
Inventory.prototype.items = function() {
return this.item_list.sort(function(a,b) {
return a.price > b.price;
});
};
Inventory.prototype.addItem = function(name, price) {
this.item_list.push({'name': name, 'price': price});
};
Inventory.prototype.removeItem = function(name) {
this.item_list = this.item_list.filter(function(obj) {
return obj.name != name;
});
};
Inventory.prototype.getItemByName = function(name) {
for (var i =0; i < this.item_list.length;i++) {
if (this.item_list[i].name == name) return this.item_list[i];
}
return null;
};
Inventory.prototype.getItemByPrice = function(price) {
for (var i =0; i < this.item_list.length;i++) {
if (this.item_list[i].price == price) return this.item_list[i];
}
return null;
};
var inventory = new Inventory();
exports.items = function(input) {
return inventory.items();
};
exports.addItem = function(name, price) {
inventory.addItem(name,price);
};
exports.getMostExpensive = function() {
var items = inventory.items();
return items[items.length -1];
};
exports.getLeastExpensive = function() {
var items = inventory.items();
return items[0];
};
exports.removeItem = function(name) {
inventory.removeItem(name);
};
exports.getItemByName = function(name) {
return inventory.getItemByName(name);
};
exports.getItemByPrice = function(price) {
return inventory.getItemByPrice(price);
};
})();
output:
Item eggs costs 1.2
Item orange juice costs 2.4
Item milk costs 4
orange juice at 2.4 costs the most!
eggs at 1.2 costs the least!
eggs with price of 1.2 retrieved by name
orange juice with price of 2.4 retrieved by price
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.