This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Übung 10: Einkaufspreis</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
|
||||
<script src="index.js" defer></script>
|
||||
<script src="solution.js" defer></script>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
function calculateTotalPrice(items = []) {
|
||||
// let total = 0;
|
||||
// items.forEach((item) => {
|
||||
// total += item.price;
|
||||
// });
|
||||
const total = items.reduce((sum, item) => {
|
||||
// Ensure that item.price is a number
|
||||
const price = typeof item.price === 'number' ? item.price : Number(item.price);
|
||||
|
||||
if (isNaN(price)) {
|
||||
console.warn(`Invalid price for item "${item.name}". Skipping...`);
|
||||
return sum;
|
||||
}
|
||||
return sum + Number(item.price);
|
||||
}, 0);
|
||||
|
||||
return Number(total.toFixed(2));
|
||||
}
|
||||
|
||||
const shoppingCart = [
|
||||
{ name: 'Laptop', price: 999.99 },
|
||||
{ name: 'Smartphone', price: 599.99 },
|
||||
{ name: 'Headphones', cost: 199.99 },
|
||||
];
|
||||
|
||||
const totalPrice = calculateTotalPrice(shoppingCart);
|
||||
console.log('Total Price:', totalPrice);
|
||||
|
||||
function displayUserProfile(user) {
|
||||
console.log(`Name: ${user.name}`);
|
||||
console.log(`Age: ${user.age}`);
|
||||
console.log(`Email: ${user.email.toLowerCase()}`);
|
||||
}
|
||||
|
||||
const userProfile = {
|
||||
name: 'Sarah',
|
||||
age: 'twenty-five',
|
||||
email: 'SARAH@EXAMPLE.COM',
|
||||
};
|
||||
|
||||
displayUserProfile(userProfile);
|
||||
|
||||
function getFirstItemName(items) {
|
||||
return items[0].name.toUpperCase();
|
||||
}
|
||||
|
||||
console.log('First Item:', getFirstItemName(shoppingCart)); // => LAPTOP
|
||||
|
||||
const numberList = [1, 2, 3, 4, 5];
|
||||
const sum = numberList.reduce((acc, num) => acc + Number(num), 0);
|
||||
console.log('Sum:', sum); // => 15
|
||||
Reference in New Issue
Block a user