From f2b45fdcf5b344c0fc7cdefa477b32ab0894a0b3 Mon Sep 17 00:00:00 2001 From: Joe Krall Date: Wed, 29 Apr 2020 23:49:58 -0400 Subject: [PATCH 1/3] first successful version --- index.html | 3 ++ main.js | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 index.html diff --git a/index.html b/index.html new file mode 100644 index 0000000..70ecde9 --- /dev/null +++ b/index.html @@ -0,0 +1,3 @@ + + + diff --git a/main.js b/main.js index 220d2e1..db1f60a 100644 --- a/main.js +++ b/main.js @@ -1 +1,79 @@ -// write your code here to make the tests pass +var Library = function() { + + var books = []; + + var addBook = function(newBook) { + // newBook.setAttribute(title, newBook); + // newBook.setAttribute(checkout, false); + books.push(newBook); + }; + + var checkOutBook = function(outgoingBook) { + for (var i = 0; i < books.length; i++) { + if (books[i].getAttribute('title') === outgoingBook.getAttribute('title')) { + if (books[i].getAttribute('checkedOut') === false) { + books[i].setAttribute('checkedOut', true); + console.log('"' + books[i].getAttribute('title') + '" is now checked out.'); + } else { + console.log('I\'m sorry, but "' + books[i].getAttribute('title') + '" is already checked out.'); + } + } else { + ('I\'m sorry, but "' + books[i].getAttribute('title') + '" is not in the library.') + } + } + } + + var returnBook = function(returningBook) { + for (var i = 0; i < books.length; i++) { + if (books[i].getAttribute('title') === returningBook.getAttribute('title')) { + if (books[i].getAttribute('checkedOut') === true) { + books[i].setAttribute('checkedOut', false); + console.log('"' + books[i].getAttribute('title') + '" is now checked in.'); + } else { + console.log('"' + books[i].getAttribute('title') + '" is already checked in. What\'s going on here?'); + } + } else { + ('I\'m sorry, but "' + books[i].getAttribute('title') + '" is not in the library.'); + } + } + } + + return { + addBook: addBook, + checkOutBook: checkOutBook, + returnBook: returnBook + }; +}; + +var Book = function(title) { + var attributes = { + title: title, + checkedOut: false + }; + + var getAttribute = function(attribute) { + //here's the rub, I needed to make sure false was valid + if (attributes[attribute] !== null) { + return attributes[attribute]; + } + }; + + var setAttribute = function(attribute, value) { + if (attribute in attributes && attributes[attribute] !== null) { + attributes[attribute] = value; + } + }; + + return { + getAttribute: getAttribute, + setAttribute: setAttribute + }; +}; + +var library = Library(); +var book1 = Book('Hunger Games'); +var book2 = Book('Toot'); + +library.addBook(book1); +library.checkOutBook(book1); +library.returnBook(book1); From 79502f618fb473ea0ecccc86972587954f8c67ca Mon Sep 17 00:00:00 2001 From: Joe Krall Date: Wed, 29 Apr 2020 23:53:12 -0400 Subject: [PATCH 2/3] using attribute in attributes --- main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.js b/main.js index db1f60a..7bba9e6 100644 --- a/main.js +++ b/main.js @@ -53,13 +53,13 @@ var Book = function(title) { var getAttribute = function(attribute) { //here's the rub, I needed to make sure false was valid - if (attributes[attribute] !== null) { + if (attribute in attributes) { return attributes[attribute]; } }; var setAttribute = function(attribute, value) { - if (attribute in attributes && attributes[attribute] !== null) { + if (attribute in attributes) { attributes[attribute] = value; } }; From fad4c73dd27f251c3fd4806f1eb8ac7fe76e4f29 Mon Sep 17 00:00:00 2001 From: Joe Krall Date: Thu, 30 Apr 2020 23:47:53 -0400 Subject: [PATCH 3/3] added isInLibrary, var book, and hasOwnProperty --- main.js | 65 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/main.js b/main.js index 7bba9e6..81f4a1e 100644 --- a/main.js +++ b/main.js @@ -3,46 +3,59 @@ var Library = function() { var books = []; var addBook = function(newBook) { - // newBook.setAttribute(title, newBook); - // newBook.setAttribute(checkout, false); books.push(newBook); }; + // isInLibrary utilized in checkedOutBook and returnBook + var isInLibrary = function(title) { + var bookCheck = books.some(function(book) { + return book.getAttribute('title') === title; + }); + + if (bookCheck) { + return true; + } else { + console.log('I\'m sorry, but "' + title + '" is not in the library.'); + return false; + } + }; + var checkOutBook = function(outgoingBook) { for (var i = 0; i < books.length; i++) { - if (books[i].getAttribute('title') === outgoingBook.getAttribute('title')) { - if (books[i].getAttribute('checkedOut') === false) { - books[i].setAttribute('checkedOut', true); - console.log('"' + books[i].getAttribute('title') + '" is now checked out.'); + // Creating bookInLibrary for readability - thanks Sean! + var book = books[i]; + if (isInLibrary(outgoingBook.getAttribute('title'))) { + if (book.getAttribute('checkedOut') === false) { + book.setAttribute('checkedOut', true); + console.log('"' + book.getAttribute('title') + '" is now checked out.'); } else { - console.log('I\'m sorry, but "' + books[i].getAttribute('title') + '" is already checked out.'); + console.log('I\'m sorry, but "' + book.getAttribute('title') + '" is already checked out.'); } - } else { - ('I\'m sorry, but "' + books[i].getAttribute('title') + '" is not in the library.') } } - } + }; + var returnBook = function(returningBook) { for (var i = 0; i < books.length; i++) { - if (books[i].getAttribute('title') === returningBook.getAttribute('title')) { - if (books[i].getAttribute('checkedOut') === true) { - books[i].setAttribute('checkedOut', false); - console.log('"' + books[i].getAttribute('title') + '" is now checked in.'); + var book = books[i]; + if (isInLibrary(returningBook.getAttribute('title'))) { + if (book.getAttribute('checkedOut') === true) { + book.setAttribute('checkedOut', false); + console.log('"' + book.getAttribute('title') + '" is now checked in.'); } else { - console.log('"' + books[i].getAttribute('title') + '" is already checked in. What\'s going on here?'); + console.log('"' + book.getAttribute('title') + '" is already checked in. What\'s going on here?'); } - } else { - ('I\'m sorry, but "' + books[i].getAttribute('title') + '" is not in the library.'); } } - } + }; return { addBook: addBook, checkOutBook: checkOutBook, returnBook: returnBook }; + }; var Book = function(title) { @@ -52,15 +65,15 @@ var Book = function(title) { }; var getAttribute = function(attribute) { - //here's the rub, I needed to make sure false was valid - if (attribute in attributes) { + //Refactor from (attribute in attributes) - ES5 compatible + if (attributes.hasOwnProperty(attribute)) { return attributes[attribute]; } }; var setAttribute = function(attribute, value) { - if (attribute in attributes) { - attributes[attribute] = value; + if (attributes.hasOwnProperty(attribute)) { + attributes[attribute] = value; } }; @@ -69,11 +82,3 @@ var Book = function(title) { setAttribute: setAttribute }; }; - -var library = Library(); -var book1 = Book('Hunger Games'); -var book2 = Book('Toot'); - -library.addBook(book1); -library.checkOutBook(book1); -library.returnBook(book1);