Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished Workshop #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
241 changes: 119 additions & 122 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,122 +1,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(`Your item is ${item.name}`);
});
}

/**
* @param {Item[]} items - array of items
* @returns {string[]} an array of item names in all uppercase
*/
function getUppercaseNames(items) {
let itemsUp = items.map((element) => element.name.toUpperCase());
return itemsUp;
}

/**
* @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) {
const itemsFind = items.find((item) => item.id === id);
return itemsFind;
}

/**
* @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) {
for (let i = 0; i < items.length; i++) {
if (items[i].name === name) {
return items[i].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) {
const itemsArr = items.filter((item) => item.category === category);
return itemsArr;
}

/**
* @param {Item[]} items - array of items
* @returns {number} the total quantity of all items
*/
function countItems(items) {
const count = items.reduce((acc, currentValue) => acc + 1, 0);
return count;
}

/**
* @param {Item[]} items - array of items
* @returns {number} the cost of all given items
*/
function calculateTotalPrice(items) {
const total = items.reduce(
(acc, currentValue) => acc + currentValue.price,
0
);
return total;
}

// --------------------- 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));
/**
* @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));
145 changes: 145 additions & 0 deletions indexBlank.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* @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) {}

/**
* @param {Item[]} items - array of items
* @returns {string[]} an array of item names in all uppercase
*/
function getUppercaseNames(items) {}

/**
* @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) {}

/**
* @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) {}

/**
* @param {Item[]} items - array of items
* @param {string} category
* @returns {Item[]} array of items that belong to the given `category`
*/
function getItemsByCategory(items, category) {}

/**
* @returns {number} the total quantity of all items
*/
function countItems(items) {}

/**
* @param {Item[]} items - array of items
* @returns {number} the cost of all given items
*/
function calculateTotalPrice(items) {}

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