Menu
Coddy logo textTech

Als gelesen markieren

Teil des Abschnitts Logik & Ablauf der JavaScript-Journey von Coddy — Lektion 46 von 65.

challenge icon

Aufgabe

Einfach

Füge ein neues case markAsRead zu deiner switch-Anweisung hinzu. Diese Aktion soll Bücher als gelesen markieren und Bewertungen hinzufügen.

Für den markAsRead-Fall:

  1. Der Parameter currentData ist ein Objekt, das Folgendes enthält:
    • bookId (Zahl)
    • rating (Zahl von 1 bis 5)
  2. Finde das Buch mit der passenden id in der Bibliothek
  3. Überprüfe, ob die Bewertung eine Zahl zwischen 1 und 5 ist
  4. Wenn das Buch gefunden wurde und die Bewertung gültig ist:
    • Setze isRead auf true
    • Setze die Bewertung auf den angegebenen Wert
  5. Füge dem Array results entsprechende Nachrichten hinzu:
    • Book marked as read! für eine erfolgreiche Aktualisierung
    • Invalid rating! Please rate between 1 and 5 für eine ungültige Bewertung
    • Book not found! falls die Buch-id nicht existiert

Probier es selbst

// Initialize library data
const libraryData = {
    books: [
        {
            id: 1,
            title: "The Great Gatsby",
            author: "F. Scott Fitzgerald",
            year: 1925,
            genre: "Fiction",
            isRead: false,
            rating: 0,
            borrowed: false,
            borrowedBy: "",
            borrowDate: ""
        }
    ],
    readers: [
        {
            name: "John Smith",
            favoriteGenre: "Fiction",
        }
    ]
};

function manageLibrary(actions, data) {
    let results = [];
    
    for (let i = 0; i < actions.length; i++) {
        const currentAction = actions[i];
        const currentData = data[i];
        
        switch (currentAction) {
            case 'printBooks':
                results.push(libraryData.books);
                break;
            case 'printReaders':
                results.push(libraryData.readers);
                break;
            case "addBook":
                // Add a new book to the library
                let newBook = {
                    id: libraryData.books.length + 1,
                    title: currentData.title,
                    author: currentData.author,
                    year: currentData.year,
                    genre: currentData.genre,
                    isRead: false,
                    rating: 0,
                    borrowed: false,
                    borrowedBy: "",
                    borrowDate: ""
                };
                libraryData.books.push(newBook);
                results.push("Book added successfully!");
                break;
            case "searchByTitle":
                // Search for books by title
                let searchResults = [];
                for(let i = 0; i < libraryData.books.length; i++) {
                    if(libraryData.books[i].title.toLowerCase().includes(currentData.toLowerCase())) {
                        searchResults.push(libraryData.books[i]);
                    }
                }
                results.push(searchResults);
                break;

          case "filterByGenre":
                // Filter books by genre
                let genreResults = [];
                for(let i = 0; i < libraryData.books.length; i++) {
                    if(libraryData.books[i].genre === currentData) {
                        genreResults.push(libraryData.books[i]);
                    }
                }
                results.push(genreResults);
                break;
            default:
                results.push("Invalid action!");
        }
    }

    return results;
}

Alle Lektionen in Logik & Ablauf