Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is node.js. And I would like make module for the following code. \'use stri

ID: 3599028 • Letter: T

Question

This is node.js. And I would like make module for the following code.

'use strict';

const inventory = require('./inventory.js');

inventory.addItem('milk', 4.00);

inventory.addItem('eggs', 1.20);

inventory.addItem('orange juice', 2.40);

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

//We would use exports to make methods available outside the module file.

exports.myModule = function() {

'use strict';

const inventory = require('./inventory.js');

inventory.addItem('milk', 4.00);

inventory.addItem('eggs', 1.20);

inventory.addItem('orange juice', 2.40);

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`);

}

//After this, we would save this code in a file named MyModule.js

//To use this module in a separate file,

var http = require('http');

var myMod = require('./MyModule');//We need path for the module file.

http.createServer(function (req, res) {

res.writeHead(200, {'Content-Type': 'text/html'});

myMod.myModule()

res.end();

}).listen(8080);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote