Menu
Coddy logo textTech

本の追加

CoddyのJavaScriptジャーニー「論理とフロー」セクションの一部 — レッスン 43/65。

challenge icon

チャレンジ

簡単

"addBook" ケースを追加してください。このケースでは以下を行う必要があります:

  1. 以下のプロパティを保持する currentData パラメータを使用して、新しい本オブジェクトを作成します:
    • title (string)
    • author (string)
    • year (string)
    • genre (string)
  2. id を生成します(libraryData.books.length + 1 を使用)
  3. isReadratingborrowedborrowedByborrowDate に(初期データと同様の)デフォルト値を設定します
  4. 新しい本を libraryData.books 配列に追加します
  5. 文字列 Book added successfully!results 配列に追加します 

自分で試してみよう

// 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;
}

論理とフローのすべてのレッスン