本の追加
CoddyのJavaScriptジャーニー「論理とフロー」セクションの一部 — レッスン 43/65。
チャレンジ
簡単"addBook" ケースを追加してください。このケースでは以下を行う必要があります:
- 以下のプロパティを保持する
currentDataパラメータを使用して、新しい本オブジェクトを作成します:- title (string)
- author (string)
- year (string)
- genre (string)
- id を生成します(
libraryData.books.length + 1を使用) isRead、rating、borrowed、borrowedBy、borrowDateに(初期データと同様の)デフォルト値を設定します- 新しい本を
libraryData.books配列に追加します - 文字列
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;
}