Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

البحث بالعنوان

جزء من قسم Logic & Flow في رحلة JavaScript على Coddy — الدرس 44 من 65.

challenge icon

التحدي

سهل

قم بإنشاء حالة searchByTitle جديدة في جملة switch الخاصة بك. يجب أن يقوم هذا الإجراء بالبحث عن الكتب من خلال عناوينها.

بالنسبة لحالة searchByTitle:

  1. المعامل currentData هو عبارة عن سلسلة نصية (string) للبحث عنها
  2. قم بإنشاء مصفوفة فارغة searchResults لتخزين نتائج البحث
  3. قم بالمرور (Loop) عبر جميع الكتب في المكتبة
  4. لكل كتاب، تحقق مما إذا كان عنوانه يتضمن سلسلة البحث
  5. يجب أن يكون البحث غير حساس لحالة الأحرف (قم بتحويل كلا السلسلتين إلى lowercase قبل المقارنة)
  6. إذا تم العثور على تطابق، أضف الكتاب إلى مصفوفة searchResults
  7. أضف مصفوفة searchResults إلى مصفوفة 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;
            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;
            default:
                results.push("Invalid action!");
        }
    }
    
    return results;
}

جميع دروس Logic & Flow