Menu
Coddy logo textTech

既読にする

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

challenge icon

チャレンジ

簡単

switch文に新しいcase markAsReadを追加してください。このアクションは、本を既読としてマークし、評価を追加するためのものです。

markAsReadケースの処理は以下の通りです:

  1. currentDataパラメータは、以下の内容を含むオブジェクトです:
    • bookId (数値)
    • rating (1から5までの数値)
  2. ライブラリ内から一致するidを持つ本を探します。
  3. ratingが1から5の間の数値であることを検証します。
  4. 本が見つかり、かつ評価が有効な場合:
    • isReadをtrueに設定します。
    • ratingを提供された値に設定します。
  5. results配列に適切なメッセージを追加します:
    • 更新に成功した場合はBook marked as read!
    • 無効な評価の場合はInvalid rating! Please rate between 1 and 5
    • 本のidが存在しない場合はBook not found!

自分で試してみよう

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

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