0
var books = new Array();

books[0] = "Silmarillion: ";
books[1] = "Horus Rising: ";
books[2] = "Lord of the Rings: ";


var ratings = new Array();

ratings[0] = "9 ";
ratings[1] = "8 ";
ratings[2] = "7 ";


function printBooks()
{
    for(var i = 0; i < books.length; i++)
    {
    document.writeln(books[i] +" " + ratings[i]);
    }

}


printBooks();

I have this code and it (basically) achieves what I want it to do but it's quite bad. I'd like to have it in two functions, one function (addBooks(title, rating)) to add my books and one function (printBooks()) to print them. I'd like the user to be asked to add one book, ask the rating of it and do this three times. After this I would like it to be printed.

I've tried to do it but I don't know how to write a function that adds into the arrays. I've only figured out I should use push() but not any more.. I hope it's clear enough.

EDIT

On top of this I would also like to have a way to calculate the average score of the books added. The code I got now for this is:

function averageRating ()
{
var sum = 0;
for(var u = 0; u < ratings.length; u++)
{
    sum += parseInt(ratings[u]);
}
var avg = sum/ratings.length;
document.writeln("<br>Number of books read: " + ratings.length + "<br>The average rating of the books are: " + avg);

averageRating();

1 Answer 1

5

The concept is you have a library with some books. So an OOP approach may be create a class Library with some method. Like:

function Library() {
    var books = [];
    this.addBook = function(bookName,rate) {
        books.push({name:bookName, rating:rate});
    }
    this.printBooks = function() {
        for(var i = 0; i < books.length; i++)  {
            document.writeln(books[i].name +": " + books[i].rating);
        }
    }
}

code:

var myLibrary = new Library();
myLibrary.addBook('Silmarillion',9);
myLibrary.addBook('Horus Rising',8);
myLibrary.addBook('Lord of the Rings',7);

myLibrary.printBooks();

However, it can be improved. You can create a class Book:

function Book(name,options) {
    this.name = name;
    this.rating = options.rating;
}

and class Library becomes:

function Library() {
    var books = [];
    this.addBook = function(book) {
        books.push(book);
    }
    this.printBooks = function() {
        for(var i = 0; i < books.length; i++)  {
            document.writeln(books[i].name +": " + books[i].rating);
        }
    }
    // EDIT: ADDED for new features required
    this.countBook = function() {
          return books.length;
    }
    this.calculateRatingAverage = function() {
         var sum = 0;
         for(var i = 0; i < books.length; i++) {
            sum += books[i].rating;
         }
        return sum/books.length;
     }
}

and its use becomes:

var myLibrary = new Library();
myLibrary.addBook(new Book('Silmarillion',{rating:9}));
myLibrary.addBook(new Book('Horus Rising',{rating:8}));
myLibrary.addBook(new Book('Lord of the Rings',{rating:7}));

myLibrary.printBooks();
document.writeln("<br>Number of books read: " + myLibrary.countBook() + "<br>The average rating of the books are: " + myLibrary.calculateRatingAverage());

So you have a solid structure that you can optimize and work on it.

Sign up to request clarification or add additional context in comments.

8 Comments

This seems to do the trick.. I'd just like you to help me add a function that writes out the average rating of the books as well to make it all complete :)
Creating the Book object as a parameter seems a little unintuitive to me. It would be much neater if the object was created inside the addBook method and only the data is passed as the parameter.
@amustill the object Book has sense if the problem becomes more complex. So you have an object that you can complicate as you want. It's the reason why it's a complement of the answer
@chumkiu A good solid answer with details. I deleted mine as a result :)
Indeed a very very good answer. On my code on the top I also had a function to add the average rating and I would love to have this as well to the code above. I've updated the question with the code I used.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.