From 37f6499e64b096c26b01706dcf083e5c55ec7eb7 Mon Sep 17 00:00:00 2001 From: ADH <61984231+adholton@users.noreply.github.com> Date: Wed, 29 Apr 2020 08:14:29 -0400 Subject: [PATCH 1/5] jasmine tests passed but not refactored --- main.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/main.js b/main.js index 220d2e1..a1e031f 100644 --- a/main.js +++ b/main.js @@ -1 +1,93 @@ -// write your code here to make the tests pass +/* + This is a factory function that returns a library object with 3 methods: + library.addBook(bookObj) - adds a book to the books array + library.checkoutBook(bookObj) - checks out a book + library.returnBook(bookObj) - checks in a book + */ +var Library = function() { + var books = []; + + //this is going to add a book object to the books array + var addBook = function(book) { + books.push(book); + } + + //this function will check-out a book if it exists in the books array and is checked in + var checkOutBook = function(book) { + + //this will check if the book is in the books array + let exists = false; + for (let i = 0; i < books.length; i++) { + if (books[i].getAttribute('title') === book.getAttribute('title')) { + exists = true; + } + } + + //if the book is checked-in and exists in the library, the book will be checked out + if (book.getAttribute('checkedOut') === false && exists) { + book.setAttribute('checkedOut', true); + } else { + console.log('This book is not available at this library'); + } + }; + + //this function will check-in a book if it exists in the books array and is checked out + var returnBook = function(book) { + + //this will check if the book is in the books array + let exists = false; + for (let i = 0; i < books.length; i++) { + if (books[i].getAttribute('title') === book.getAttribute('title')) { + exists = true; + } + } + + //if the book is checked out and exists in the library, the book will be checked in + if (book.getAttribute('checkedOut') && exists) { + book.setAttribute('checkedOut', false); + } else { + console.log('This book is not available at this library'); + } + }; + + //this returns the library object with 3 methods for handling books, using closure to manipulate the books array in ONLY 3 ways and not directly + return { + addBook: addBook, + checkOutBook: checkOutBook, + returnBook: returnBook + }; +} + +/* +This is a factory function that returns a book object with 2 methods: +book.getAttribute('title') - returns the title +book.setAttribute('checkedOut', true) - changes checkout status +*/ +var Book = function(title, author) { + var attributes = { + title: title, + author: author, + checkedOut: false + }; + + //if the attribute exists on the book, this will return the value + var getAttribute = function(attribute) { + if (attributes[attribute] === undefined) { + console.error(`The property ${attribute} is undefined`); + } + return attributes[attribute]; + }; + + //if the attribute exists on the book, this will reset the value + var setAttribute = function(attribute, value) { + if (attributes[attribute] !== undefined) { + attributes[attribute] = value; + } + } + + //this returns the book object with only 2 methods. we can only access and change properties that already exist on the book + return { + getAttribute: getAttribute, + setAttribute: setAttribute + }; +}; \ No newline at end of file From 2787eb3db7f819f9e4617193a6fa6a4dd65bd922 Mon Sep 17 00:00:00 2001 From: ADH <61984231+adholton@users.noreply.github.com> Date: Wed, 29 Apr 2020 08:29:04 -0400 Subject: [PATCH 2/5] removed error throw if prop not defined --- main.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index a1e031f..780c9a7 100644 --- a/main.js +++ b/main.js @@ -72,10 +72,9 @@ var Book = function(title, author) { //if the attribute exists on the book, this will return the value var getAttribute = function(attribute) { - if (attributes[attribute] === undefined) { - console.error(`The property ${attribute} is undefined`); + if (attributes[attribute] !== undefined) { + return attributes[attribute]; } - return attributes[attribute]; }; //if the attribute exists on the book, this will reset the value From acb84488ada2bff267ef0100d9cb4171f9ee1125 Mon Sep 17 00:00:00 2001 From: ADH <61984231+adholton@users.noreply.github.com> Date: Wed, 29 Apr 2020 18:37:08 -0400 Subject: [PATCH 3/5] added comments --- main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.js b/main.js index 780c9a7..e5d7927 100644 --- a/main.js +++ b/main.js @@ -7,7 +7,7 @@ var Library = function() { var books = []; - //this is going to add a book object to the books array + //this adds a book object to the books array var addBook = function(book) { books.push(book); } @@ -84,7 +84,7 @@ var Book = function(title, author) { } } - //this returns the book object with only 2 methods. we can only access and change properties that already exist on the book + //this returns the book object with only 2 methods. we can only access and change properties that ALREADY exist on the book return { getAttribute: getAttribute, setAttribute: setAttribute From edd3568b50b2867adfe517461cea0e320a96ca76 Mon Sep 17 00:00:00 2001 From: ADH <61984231+adholton@users.noreply.github.com> Date: Wed, 29 Apr 2020 18:58:17 -0400 Subject: [PATCH 4/5] refactored code, passed tests --- main.js | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/main.js b/main.js index e5d7927..bdc235a 100644 --- a/main.js +++ b/main.js @@ -12,38 +12,18 @@ var Library = function() { books.push(book); } - //this function will check-out a book if it exists in the books array and is checked in + //this function will check-out a book if it exists in the books array var checkOutBook = function(book) { - - //this will check if the book is in the books array - let exists = false; - for (let i = 0; i < books.length; i++) { - if (books[i].getAttribute('title') === book.getAttribute('title')) { - exists = true; - } - } - - //if the book is checked-in and exists in the library, the book will be checked out - if (book.getAttribute('checkedOut') === false && exists) { + if (books.includes(book)) { book.setAttribute('checkedOut', true); } else { console.log('This book is not available at this library'); } }; - //this function will check-in a book if it exists in the books array and is checked out + //this function will check-in a book if it exists in the books array var returnBook = function(book) { - - //this will check if the book is in the books array - let exists = false; - for (let i = 0; i < books.length; i++) { - if (books[i].getAttribute('title') === book.getAttribute('title')) { - exists = true; - } - } - - //if the book is checked out and exists in the library, the book will be checked in - if (book.getAttribute('checkedOut') && exists) { + if (books.includes(book)) { book.setAttribute('checkedOut', false); } else { console.log('This book is not available at this library'); @@ -64,6 +44,7 @@ book.getAttribute('title') - returns the title book.setAttribute('checkedOut', true) - changes checkout status */ var Book = function(title, author) { + //this obj stores 3 properties on a new book var attributes = { title: title, author: author, @@ -84,7 +65,7 @@ var Book = function(title, author) { } } - //this returns the book object with only 2 methods. we can only access and change properties that ALREADY exist on the book + //this returns the book object with 2 methods to access the attributes return { getAttribute: getAttribute, setAttribute: setAttribute From 047abddec1346bbf2a48d8f2aa82940b20517398 Mon Sep 17 00:00:00 2001 From: ADH <61984231+adholton@users.noreply.github.com> Date: Thu, 30 Apr 2020 08:46:52 -0400 Subject: [PATCH 5/5] add error check for title --- main.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/main.js b/main.js index bdc235a..86961c4 100644 --- a/main.js +++ b/main.js @@ -30,7 +30,7 @@ var Library = function() { } }; - //this returns the library object with 3 methods for handling books, using closure to manipulate the books array in ONLY 3 ways and not directly + //this returns the library object with 3 methods to manipulate the books array return { addBook: addBook, checkOutBook: checkOutBook, @@ -45,11 +45,16 @@ book.setAttribute('checkedOut', true) - changes checkout status */ var Book = function(title, author) { //this obj stores 3 properties on a new book - var attributes = { - title: title, - author: author, - checkedOut: false - }; + //title argument is required create/return book object + if (title) { + var attributes = { + title: title, + author: author || null, //null if author argument is absent + checkedOut: false + } + } else { + return console.log('Book title is required!'); + } //if the attribute exists on the book, this will return the value var getAttribute = function(attribute) {