Question

searchBooks: Write a JavaScript function to find ALL the books in a 'library' written by a...

searchBooks: Write a JavaScript function to find ALL the books in a 'library' written by a specific author. You should return:

  • The list of the book titles as a string, with titles separated by a comma; OR
  • 'NOT FOUND' if there is no match.
  • Note that there should be no trailing comma and no additional space(s) surrounding the comma(s) - see the examples below for details.
    Example dataset, input and expected output:
    library = [ 
       { author: 'Bill Gates', title: 'The Road Ahead', libraryID: 1254},
       { author: 'Carolann Camilo', title: 'Eyewitness', libraryID: 32456},
       { author: 'Carolann Camilo', title: 'Cocky Marine', libraryID: 32457}  
      ];
    
    Input: [library, 'Bill Gates']
    Expected Output: 'The Road Ahead'
    
    Input: [library, 'Carolann Camilo']
    Expected Output: 'Eyewitness,Cocky Marine'
    
    Input: [library, 'Lala']
    Expected Output: 'NOT FOUND'
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the answer...

CODE:

function searchBooks(library, authorName) {

    var result="";

    var count=0;

    for(var i=0;i<library.length;i++){          //traverse array

        if(library[i].author==authorName){      //check if author present in list

            // console.log(library[i].author);

            if(count>=1)

                result+=",";                    //to add comma for if multiple values present

            result+=library[i].title;           //add to result variable

            count++;        

        }

    }

    if(result=="")

        return "NOT FOUND";

    return result;

}

library=[{author:'Bill Gates', title: 'The Road Ahead', libraryID:1254},

{ author: 'Carolann Camilo', title: 'The Way AHead', libraryID:1245},

{author:'Bill Gates', title: 'Second Book', libraryID:1255},

];

console.log(searchBooks(library,"Bill Gates"));

console.log(searchBooks(library,"Stark"));

console.log(searchBooks(library,"Carolann Camilo"));


OUTPUT:

If you have any doubts please COMMENT...

If you understand the answer please give THUMBS UP....

Add a comment
Know the answer?
Add Answer to:
searchBooks: Write a JavaScript function to find ALL the books in a 'library' written by a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Exceptions Suppose a library is processing an input file containing the titles of books in...

    Java Exceptions Suppose a library is processing an input file containing the titles of books in order to remove duplicates. Write a program that reads all of the titles from an input file called bookTitles.inp and writes them to an output file called uniqueTitles.out. When complete, the output file should contain all unique titles found in the input file. Create the input file using Notepad or another text editor, with one title per line. Make sure you have a number...

  • // C programming Create a system managing a mini library system. Every book corresponds to a...

    // C programming Create a system managing a mini library system. Every book corresponds to a record (line) in a text file named "mylibrary.txt". Each record consists of 6 fields (Book ID, Title, Author, Possession, checked out Date, Due Date) separated by comma: No comma '', "in title or author name. This mini library keeps the record for each book in the library. Different books can share the book "Title". But the "Book ID" for each book is unique. One...

  • Please provide original Answer, I can not turn in the same as my classmate. thanks In...

    Please provide original Answer, I can not turn in the same as my classmate. thanks In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a title, author, and an ISBN number. You will create 2 classes: Textbook and Library. Textbook class should have all above attributes and also a “next” pointer. Textbook Type Attribute String title String author String ISBN Textbook* next Textbook Type Attribute String title String...

  • Part 1 The purpose of this part of the assignment is to give you practice in...

    Part 1 The purpose of this part of the assignment is to give you practice in creating a class. You will develop a program that creates a class for a book. The main program will simply test this class. The class will have the following data members: A string for the name of the author A string for the book title A long integer for the ISBN The class will have the following member functions (details about each one are...

  • Don't attempt if you can't attempt fully, i will dislike a nd negative comments would be...

    Don't attempt if you can't attempt fully, i will dislike a nd negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book tit le, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an...

  • Don't attempt if you can't attempt fully, i will dislike and negative comments would be given...

    Don't attempt if you can't attempt fully, i will dislike and negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book titnle, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an HTML web...

  • // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given...

    // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given a partially completed program that creates a list of patients, like patients' record. // Each record has this information: patient's name, doctor's name, critical level of patient, room number. // The struct 'patientRecord' holds information of one patient. Critical level is enum type. // An array of structs called 'list' is made to hold the list of patients. // To begin, you should trace...

  • import java.util.Arrays; import java.util.Random; import java.util.Scanner; /** * TODO Write a summary of the role of...

    import java.util.Arrays; import java.util.Random; import java.util.Scanner; /** * TODO Write a summary of the role of this class in the * MasterMind program. * * @author TODO add your name here */ public class MasterMind { /** * Prompts the user for a value by displaying prompt. * Note: This method should not add a new line to the output of prompt. * * After prompting the user, the method will consume an entire * line of input while reading...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT