Menu
Coddy logo textTech

도서 추가하기

Coddy JavaScript 여정의 논리 & 흐름 섹션에 포함된 레슨 — 65개 중 43번째.

challenge icon

챌린지

쉬움

"addBook" 케이스를 추가하세요. 이 케이스는 다음을 수행해야 합니다:

  1. 다음 속성들을 포함하는 currentData 매개변수를 사용하여 새로운 도서 객체를 생성합니다:
    • title (string)
    • author (string)
    • year (string)
    • genre (string)
  2. id를 생성합니다 (libraryData.books.length + 1 사용)
  3. isRead, rating, borrowed, borrowedBy, borrowDate에 대한 기본값을 설정합니다 (초기 데이터와 동일하게)
  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;
}

논리 & 흐름의 모든 레슨