JavaScript III - Zadanie 4
Używając omawianych do tej pory funkcji i metod tablicowych oraz podanego kodu uzyskaj następującą funkcjonalność:
● funkcja dodająca nowy przedmiot do tablicy uwzględniając dane podawane przez użytkownika lub używająca domyślnych (id dodawane automatycznie na podstawie długości tablicy)
● funkcja pokazująca przedmioty z danej kategorii
● funkcja kalkulująca nową cenę przedmiotu na podstawie informacji czy przedmiot jest na wyprzedaży (promoSale) i wyliczająca jego nową cenę na podstawie bieżącej oraz pola promoOff
Dla chętnych:
● funkcja modyfikująca przedmiot po podaniu id i właściwości do modyfikacji
● funkcja usuwająca przedmiot po podaniu id
● funkcja sortująca przedmioty alfabetycznie
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Sklep internetowy</title>
</head>
<body>
<script>
const shopItems = [
// ======= LAPTOPY =======
{
itemId: 1,
itemName: "Laptop Pro 15",
itemPrice: 4999.99,
itemTax: 0.23,
isOnStock: true,
promoSale: true,
promoOff: 0.1,
itemManufacturer: "TechCorp",
itemCategory: "Laptopy",
},
{
itemId: 2,
itemName: "Notebook Air 13",
itemPrice: 3499.0,
itemTax: 0.23,
isOnStock: true,
promoSale: false,
promoOff: 0.0,
itemManufacturer: "SkyLine",
itemCategory: "Laptopy",
},
{
itemId: 3,
itemName: "Gaming Beast 17",
itemPrice: 6999.99,
itemTax: 0.23,
isOnStock: false,
promoSale: true,
promoOff: 0.15,
itemManufacturer: "IronTech",
itemCategory: "Laptopy",
},
{
itemId: 4,
itemName: "OfficeBook 14",
itemPrice: 2799.0,
itemTax: 0.23,
isOnStock: true,
promoSale: false,
promoOff: 0.0,
itemManufacturer: "WorkPro",
itemCategory: "Laptopy",
},
// ======= MONITORY =======
{
itemId: 5,
itemName: 'Monitor 27" 144Hz',
itemPrice: 999.0,
itemTax: 0.23,
isOnStock: true,
promoSale: true,
promoOff: 0.15,
itemManufacturer: "VisionX",
itemCategory: "Monitory",
},
{
itemId: 6,
itemName: 'Monitor 24" IPS',
itemPrice: 599.0,
itemTax: 0.23,
isOnStock: true,
promoSale: false,
promoOff: 0.0,
itemManufacturer: "DisplayPro",
itemCategory: "Monitory",
},
{
itemId: 7,
itemName: 'Monitor 32" 4K',
itemPrice: 1599.0,
itemTax: 0.23,
isOnStock: false,
promoSale: true,
promoOff: 0.2,
itemManufacturer: "UltraView",
itemCategory: "Monitory",
},
{
itemId: 8,
itemName: 'Monitor Curved 29"',
itemPrice: 1299.0,
itemTax: 0.23,
isOnStock: true,
promoSale: false,
promoOff: 0.0,
itemManufacturer: "WaveScreen",
itemCategory: "Monitory",
},
// ======= AKCESORIA =======
{
itemId: 9,
itemName: "Klawiatura Mechaniczna RGB",
itemPrice: 349.0,
itemTax: 0.23,
isOnStock: true,
promoSale: true,
promoOff: 0.05,
itemManufacturer: "KeyMaster",
itemCategory: "Akcesoria",
},
{
itemId: 10,
itemName: "Mysz Bezprzewodowa",
itemPrice: 129.99,
itemTax: 0.23,
isOnStock: true,
promoSale: false,
promoOff: 0.0,
itemManufacturer: "PointerTech",
itemCategory: "Akcesoria",
},
{
itemId: 11,
itemName: "Podkładka XXL",
itemPrice: 79.99,
itemTax: 0.23,
isOnStock: true,
promoSale: true,
promoOff: 0.1,
itemManufacturer: "SoftBoard",
itemCategory: "Akcesoria",
},
{
itemId: 12,
itemName: "Hub USB-C 7w1",
itemPrice: 159.0,
itemTax: 0.23,
isOnStock: false,
promoSale: false,
promoOff: 0.0,
itemManufacturer: "ConnectMe",
itemCategory: "Akcesoria",
},
// ======= AUDIO =======
{
itemId: 13,
itemName: "Słuchawki Bezprzewodowe",
itemPrice: 299.0,
itemTax: 0.23,
isOnStock: true,
promoSale: true,
promoOff: 0.12,
itemManufacturer: "SoundBeat",
itemCategory: "Audio",
},
{
itemId: 14,
itemName: "Głośnik Bluetooth",
itemPrice: 199.0,
itemTax: 0.23,
isOnStock: true,
promoSale: false,
promoOff: 0.0,
itemManufacturer: "BassPro",
itemCategory: "Audio",
},
{
itemId: 15,
itemName: "Soundbar 2.1",
itemPrice: 699.0,
itemTax: 0.23,
isOnStock: false,
promoSale: true,
promoOff: 0.1,
itemManufacturer: "CinemaWave",
itemCategory: "Audio",
},
{
itemId: 16,
itemName: "Mikrofon USB",
itemPrice: 249.0,
itemTax: 0.23,
isOnStock: true,
promoSale: false,
promoOff: 0.0,
itemManufacturer: "VoiceLink",
itemCategory: "Audio",
},
// ======= SMART HOME =======
{
itemId: 17,
itemName: "Żarówka Smart LED",
itemPrice: 49.99,
itemTax: 0.23,
isOnStock: true,
promoSale: false,
promoOff: 0.0,
itemManufacturer: "BrightHome",
itemCategory: "Smart Home",
},
{
itemId: 18,
itemName: "Kamera IP WiFi",
itemPrice: 229.0,
itemTax: 0.23,
isOnStock: true,
promoSale: true,
promoOff: 0.08,
itemManufacturer: "SecureCam",
itemCategory: "Smart Home",
},
{
itemId: 19,
itemName: "Gniazdko Smart",
itemPrice: 69.99,
itemTax: 0.23,
isOnStock: false,
promoSale: false,
promoOff: 0.0,
itemManufacturer: "PlugSmart",
itemCategory: "Smart Home",
},
{
itemId: 20,
itemName: "Stacja Pogodowa WiFi",
itemPrice: 199.0,
itemTax: 0.23,
isOnStock: true,
promoSale: true,
promoOff: 0.1,
itemManufacturer: "WeatherTech",
itemCategory: "Smart Home",
},
];
console.table(shopItems);
function addItem({
itemName = "Nowy przedmiot",
itemPrice = 0,
itemTax = 0.23,
isOnStock = true,
promoSale = false,
promoOff = 0.0,
itemManufacturer = "Brak producenta",
itemCategory = "Inne",
} = {}) {
const newId = shopItems.length + 1;
const newItem = {
itemId: newId,
itemName,
itemPrice,
isOnStock,
promoSale,
promoOff,
itemManufacturer,
itemCategory,
};
shopItems.push(newItem);
console.log("Dodano nowy przedmiot");
console.table([newItem]);
}
addItem({
itemName: "Laptop Lenovo",
itemPrice: 5000,
itemTax: 0.23,
isOnStock: true,
promoSale: false,
promoOff: 0.1,
itemManufacturer: "Lenovo",
itemCategory: "Laptopy",
});
console.table(shopItems);
function showProductsFromCategory(category) {
const categoryItems = shopItems.filter(
(item) => item.itemCategory === category
);
return console.table(categoryItems);
}
showProductsFromCategory("Laptopy");
function calculateNewPrice(product) {
const productToFind = shopItems.find(
(item) => item.itemName === product
);
if (productToFind.promoSale === true) {
const newPrice =
productToFind.itemPrice * (1 - productToFind.promoOff);
return console.log(newPrice);
} else {
return console.log("Produkt nie jest na promocji");
}
}
calculateNewPrice("Laptop Pro 15");
function updateDate(id, { changes } = {}) {
const index = shopItems.findIndex((item) => item.itemId === id);
if (index === -1) {
return console.log("Nie znaleziono przedmiotu o id: ", id);
}
shopItems[index] = {
...shopItems[index],
...changes,
};
console.log("Znaleziono produkt");
console.table([shopItems[index]]);
}
updateDate(20, {
changes: { itemName: "Laptop Lenovo 2", isOnStock: false },
});
function deleteProduct(id) {
const productToDeleteIndex = shopItems.findIndex(
(item) => item.itemId === id
);
const productToDelete = shopItems.splice(productToDeleteIndex, 1);
return console.table(shopItems);
}
deleteProduct(5);
function sortProductsByNames() {
shopItems.sort((a, b) => a.itemName.localeCompare(b.itemName));
console.table(shopItems);
}
sortProductsByNames();
</script>
</body>
</html>