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

This is building a node.js module that is designed to track inventoru for a stor

ID: 3805960 • Letter: T

Question

This is building a node.js module that is designed to track inventoru for a store. I need it as code.

I need the inventory 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.

The module should not expose any functions or bariables that would allow the above functions to have the integrityof their data compromised.

Explanation / Answer

/* Inventory Methods */

/*The following code demonstrates the module which is used to implement the Inventory Methods:
1) Method addItem
2) Method getMostExpensive
3) Method getLeasrExpensive
4) Method remove
5) Method getItemByName
6) Method getItemByPrice
*/

// The module can be imported into any of the files for implementing these methods
var method={}; // Declaration of the object which will refer to all the methods

/*Arr is the array (list of inventory item) on which the implementation is done which needs to be passed as an argument in every function
so that the inventory list can be maintained */
method.addItem=function(itemPrice,itemName,Arr) // Method addItem
{
   Arr.push([itemPrice,itemName]);
}
method.items=function(Arr) // Method items
{
   var Arr1=Arr;
   Arr1.sort(function(a,b) {
return a[0] - b[0];
});
   return Arr1;
}
method.getMostExpensive=function(Arr) // Method getMostExpensive
{
   Arr1.sort(function(a,b) {
return a[0] - b[0];
});
   var obj=
   {
       "ItemName" : Arr[Arr.length-1][0],
       "ItemPrice" : Arr[Arr.length-1][1]
   }
   return obj;
}
method.getLeastExpensive=function(Arr) // Method getLeasrExpensive
{
   Arr1.sort(function(a,b) {
return a[0] - b[0];
});
   var obj=
   {
       "ItemName" : Arr[0][0],
       "ItemPrice" : Arr[0][1]
   }
   return obj;
}
method.remove=function(itemName,Arr) // Method remove
{
   var index;
   for(var i=0;i<Arr.length;i++)
   {
       if(Arr[i][1]==itemName)
       {
           index=i;
       }
   }
   Arr.splice(index,1);
}
method.getItemByName=function(itemName,Arr) // Method getItemByName
{
   var obj=
   {
       "ItemName" : "",
       "ItemPrice" : ""
   }
   for(var i=0;i<Arr.length;i++)
   {
       if(Arr[i][1]==itemName)
       {
           obj.ItemName=Arr[i][1];
           obj.ItemPrice=Arr[i][0];
           break;
       }
   }
   return obj;
}
method.getItemByPrice=function(itemPrice,Arr) // Method getItemByPrice
{
   var obj=
   {
       "ItemName" : "",
       "ItemPrice" : ""
   }
   for(var i=0;i<Arr.length;i++)
   {
       if(Arr[i][0]==itemPrice)
       {
           obj.ItemName=Arr[i][1];
           obj.ItemPrice=Arr[i][0];
           break;
       }
   }
   return obj;
}

exports.data=method; // This statement is used to export the methods, so that the methods can be implemented by another program which imports this module

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