This commit is contained in:
Philippe Torrel
2026-11-03 11:40:59 +01:00
parent a885cd9550
commit baf557f666
17 changed files with 1158 additions and 19 deletions
@@ -0,0 +1,100 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.css" />
<title>Exercise: Nested If-Statements</title>
</head>
<body>
<main>
<div class="container py-5">
<h1>Exercise: Nested If-Statements</h1>
<hr />
<div class="alert alert-primary">
<h2>Übung 2: Bibliotheksverwaltung – Extraktion</h2>
<p>
Diese Übung setzt voraus, dass du die Übung Bibliotheksverwaltung abgeschlossen hast. Du kannst deine eigene
Lösung oder eine Musterlösung für die Übung verwenden.
</p>
<p>
Diesmal machst du den Code lesbarer und übersichtlicher, indem du die Kernlogik in separate Funktionen
extrahierst. Erstelle eine neue Datei, kopiere den Code hinein und wende das Extraktionskonzept an.
</p>
</div>
</div>
</main>
<script>
'use strict';
(() => {
let isLibraryUpdating = false;
let isUserLoggedIn = true;
let hasLibrarianPermissions = false;
let hasBookEditCapability = true;
let hasBookAccess = true;
const bookCollection = [
{ title: 'Book A', price: 150, quantity: 5 },
{ title: 'Book B', price: 250, quantity: 0 },
{ title: 'Book C', price: 350, quantity: 10 },
];
// init
const init = () => {
handleLibraryOperations();
};
// Functions =======
const handleLibraryOperations = () => {
if (isLibraryUpdating) {
console.log('Library system is currently updating.');
return;
}
if (!isUserLoggedIn) {
console.log('Please log in to manage books.');
return;
}
if (!hasPermissions()) {
console.log('Insufficient rights to manage books.');
return;
}
bookCollection.forEach(logBook);
};
const logBook = (book) => {
console.log(`Book Title: ${book.title}`);
logBookPrice(book);
logBookAvailability(book);
console.log('---');
};
const logBookPrice = (book, threshold = 200) => {
if (book.price > threshold) {
console.log(`Book price is greater than ${threshold}.`);
} else {
console.log(`Book price is less than or equal to ${threshold}.`);
}
};
const logBookAvailability = (book, stockAmount = 0) => {
if (book.quantity > stockAmount) {
console.log('Book is available.');
} else {
console.log('Book is unavailable.');
}
};
const hasPermissions = () => {
return hasLibrarianPermissions || (hasBookEditCapability && hasBookAccess);
};
init();
})();
</script>
</body>
</html>