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();