Menu
Coddy logo textTech

Bücher hinzufügen

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

challenge icon

Aufgabe

Einfach

Füge den Case "addBook" hinzu. Dieser Case sollte:

  1. Erstelle ein neues Buch-Objekt unter Verwendung des Parameters currentData, der die folgenden Eigenschaften enthält:
    • title (string)
    • author (string)
    • year (string)
    • genre (string)
  2. Generiere eine id (verwende libraryData.books.length + 1)
  3. Setze Standardwerte für: isRead, rating, borrowed, borrowedBy, borrowDate (wie in den initialen Daten)
  4. Füge das neue Buch dem Array libraryData.books hinzu
  5. Füge den String Book added successfully! dem Array results hinzu 

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;
            default:
                results.push("Invalid action!");
        }
    }
    
    return results;
}

Alle Lektionen in Logik & Ablauf