I'm a budding JavaScript programmer, working on an exercise where I'm trying to create two classes (Books
and Shelf
) and need to have a shelf know what books are on it. I'm pretty new to OOP so I know I'm missing a crucial step (or three). I probably already ran off the rails, but any point in the right direction would be greatly appreciated.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Library</title>
</head>
<body>
<script type="text/javascript">
//The Books
function Books(title, enshelf, unshelf) {
this.title = title;
this.enshelf = enshelf;
this.unshelf = unshelf;
}
var bookA = new Books('The War of 1812',true,false);
var bookB = new Books('Watership Down',false,true);
var bookC = new Books('Sand Cake',true,false);
//The Shelves
function Shelf (shelfNumber, shelfName) {
this.shelfNumber = shelfNumber;
this.shelfName = shelfName;
}
var shelf100 = new Shelf('100','History');
var shelf200 = new Shelf('200','Fiction');
var shelf300 = new Shelf('300','Childrens');
var shelfTotal = [shelf100, shelf200, shelf300];
var bookInventory = [bookA, bookB, bookC];
// List all the books in the library
function displayBooks(book) {
document.write(book.title + "<br>" + book.shelfName +"<br><br>");
}
//Show total number of books
var showBooks = function() {
for(i=0; i < bookInventory.length; i++) {
displayBooks(bookInventory[i]);
}
}
showBooks();
document.write("Total number of books: " + bookInventory.length);
</script>
</body>
</html>