-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
119 lines (98 loc) · 3.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* @typedef Item
* @property {number} id - this item's ID
* @property {string} name - name of this item
* @property {number} price - price of this item
* @property {string} category - the food group this item belongs to
* @property {number} quantity - number of this item in inventory
*/
// ------------------ Complete the functions written below ------------------------------ //
/**
* Prints out the name of each item in the given array.
* @param {Item[]} items - array of items
*/
function logNames(items) {
items.forEach(item => console.log(item.name));
}
/**
* @param {Item[]} items - array of items
* @returns {string[]} an array of item names in all uppercase
*/
function getUppercaseNames(items) {
return items.forEach((item) => console.log(item.name.toUpperCase()))
}
/**
* @param {Item[]} items - array of items
* @param {number} id - id of the item to find
* @returns {Item} - the item in `items` with the given `id`
*/
function getItemById(items, id) {
return items.find(item => item.id === id);
}
/**
* @param {Item[]} items - array of items
* @param {string} name - name of the item to find
* @returns {number} the price of the item named `name`
*/
function getItemPriceByName(items, name) {
const price = items.find(item => name === item.name)
return price ? price.price : undefined
// 1. find item by its name
// 2. use item name to get its price
// 3. return item price
}
/**
* @param {Item[]} items - array of items
* @param {string} category
* @returns {Item[]} array of items that belong to the given `category`
*/
function getItemsByCategory(items, category) {
return items.filter(item => category === item.category )
// 1. use filter to find the items
// 2. return items
}
/**
* @returns {number} the total quantity of all items
*/
function countItems(items) {
return items.reduce((sum, num) => sum + num.quantity, 0);
// 1. use reduce to add the sum of the quantites
// 2. return sum
}
/**
* @param {Item[]} items - array of items
* @returns {number} the cost of all given items
*/
function calculateTotalPrice(items) {
return items.reduce((sum, num) => sum + num.price, 0)
}
// --------------------- DO NOT CHANGE THE CODE BELOW ------------------------ //
/** @type {Item[]} */
const INVENTORY = [
{ id: 1, name: "apple", price: 1.75, category: "fruit", quantity: 100 },
{ id: 2, name: "banana", price: 0.25, category: "fruit", quantity: 137 },
{ id: 3, name: "orange", price: 1.0, category: "fruit", quantity: 10 },
{ id: 4, name: "broccoli", price: 3.0, category: "vegetable", quantity: 67 },
{ id: 6, name: "milk", price: 5.75, category: "dairy", quantity: 90 },
{ id: 7, name: "cheddar", price: 4.0, category: "dairy", quantity: 63 },
{ id: 8, name: "sourdough", price: 5.5, category: "grains", quantity: 81 },
];
console.log("Welcome! We carry the following items:");
logNames(INVENTORY);
console.log("Here are the names again in all uppercase:");
console.log(getUppercaseNames(INVENTORY));
console.log(`In total, we have ${countItems(INVENTORY)} items in stock.`);
const totalCost = calculateTotalPrice(INVENTORY);
console.log(
`It would cost $${totalCost?.toFixed(2)} to purchase everything in stock.`
);
const itemId = prompt("Enter the ID of an item:", "1");
console.log(`The item with id #${itemId} is:`);
console.log(getItemById(INVENTORY, +itemId));
const itemName = prompt("Enter the name of an item:", "apple");
console.log(
`The price of ${itemName} is ${getItemPriceByName(INVENTORY, itemName)}.`
);
const category = prompt("Enter a category you would like to see:", "fruit");
console.log(`The items in the ${category} category are:`);
console.log(getItemsByCategory(INVENTORY, category));