Last active
July 21, 2016 21:04
-
-
Save MariaSzubski/2d9253aadcaba25f315f to your computer and use it in GitHub Desktop.
Using a constructor as a template for new javascript objects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------------------------------------- Function that will be used as a template | |
| function Book(title, author, alreadyRead) { | |
| this.title = title; | |
| this.author = author; | |
| this.alreadyRead = alreadyRead; | |
| } | |
| // ------------------------------------- New objects created from Book() | |
| var bookArray = [ | |
| new Book("Dune", "Frank Herbert", false), | |
| new Book("Sandstorm", "James Rollins", true), | |
| new Book("The Maze Runner", "James Dashner", false), | |
| new Book("Extremely Loud and Incredibly Close", "Jonathan Safran Foer", true), | |
| new Book("World War Z", "Max Brooks", true), | |
| new Book("Armada", "Ernest Cline", false) | |
| ]; | |
| // ------------------------------------- Loop though bookArray to evaluate if the book has been read | |
| var readCheck; | |
| for (var i = 0; i < bookArray.length; i++) { | |
| if (bookArray[i].alreadyRead === true) { | |
| readCheck = 'You already read '; | |
| } else { | |
| readCheck = 'You still need to read '; | |
| } | |
| console.log(readCheck + '"' + bookArray[i].title + '" by ' + bookArray[i].author +'.'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment