Complete these exercises after the session. All exercises use the tea data.
import { teas } from "../data/teas.js";This code uses a traditional for-loop. Rewrite it using filter and map with arrow functions:
const result = [];
for (let i = 0; i < teas.length; i++) {
if (teas[i].caffeineLevel !== "none") {
result.push(teas[i].name.toUpperCase());
}
}
console.log(result);Your solution should be 2-3 lines using chained array methods.
Build a function that generates an inventory report:
function inventoryReport(teas) {
return {
totalTeas: /* total number of teas */,
inStock: /* number of teas where inStock is true */,
outOfStock: /* number of teas where inStock is false */,
totalInventoryValue: /* sum of (pricePerGram * stockCount) for all teas */,
averagePrice: /* average pricePerGram across all teas */
};
}
console.log(inventoryReport(teas));Expected output structure:
{
totalTeas: 20,
inStock: 18,
outOfStock: 2,
totalInventoryValue: 234.55, // example number
averagePrice: 0.21 // example number
}Create a function that returns teas with low stock (less than 50 items):
function lowStockAlert(teas) {
// Return array of objects with name and stockCount
// sorted by stockCount (lowest first)
}
console.log(lowStockAlert(teas));Expected output format:
[
{ name: "Silver Needle", stockCount: 25 },
{ name: "Matcha", stockCount: 30 },
// ...
];Create a function that groups teas by their origin country:
function teasByOrigin(teas) {
// Return object where keys are origins and values are arrays of tea names
}
console.log(teasByOrigin(teas));Expected output format:
{
Japan: ["Sencha", "Matcha", "Gyokuro", "Genmaicha"],
India: ["Earl Grey", "Darjeeling", "Assam"],
China: ["Dragon Well", "White Peony", ...],
// ...
}Create a search function for the tea shop:
function searchTeas(teas, query) {
// Return teas where the name contains the query (case-insensitive)
// Return just the names, sorted alphabetically
}
console.log(searchTeas(teas, "earl"));
// Returns: ["Earl Grey"]
console.log(searchTeas(teas, "dragon"));
// Returns: ["Dragon Well"]
console.log(searchTeas(teas, "ch"));
// Returns: ["English Breakfast", "Genmaicha", "Lapsang Souchong"]Hint: Use .toLowerCase() and .includes() for case-insensitive search.
These exercises use reduce. Try them if you want a challenge!
Calculate the total value of all tea inventory using reduce:
const totalValue = teas.reduce((sum, tea) => {
// add pricePerGram * stockCount to sum
}, 0);
console.log("Total inventory value:", totalValue);Hint: reduce builds up a single value by processing each item. The 0 is your starting value.
Use reduce to count how many teas of each type exist:
const countByType = teas.reduce((counts, tea) => {
// increment counts[tea.type]
}, {});
console.log(countByType);
// Expected: { green: 6, black: 6, herbal: 4, oolong: 2, white: 2 }Create a folder week1-assignment/ with:
exercise1.js- Rewrite with array methodsexercise2.js- Inventory reportexercise3.js- Low stock alertexercise4.js- Teas by originexercise5.js- Search functionexercise6.js- Total inventory value (optional)exercise7.js- Count by type (optional)
Each file should be runnable with node exerciseN.js.
// Example structure for exercise1.js
import { teas } from "../data/teas.js";
const result = teas.filter(/* ... */).map(/* ... */);
console.log(result);