Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<html>
<script src="main.js"></script>
</html>
85 changes: 84 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
@@ -1 +1,84 @@
// write your code here to make the tests pass
var Library = function() {

var books = [];

var addBook = function(newBook) {
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++) {
// 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 "' + book.getAttribute('title') + '" is already checked out.');
}
}
}
};


var returnBook = function(returningBook) {
for (var i = 0; i < books.length; i++) {
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('"' + book.getAttribute('title') + '" is already checked in. What\'s going on here?');
}
}
}
};

return {
addBook: addBook,
checkOutBook: checkOutBook,
returnBook: returnBook
};

};

var Book = function(title) {
var attributes = {
title: title,
checkedOut: false
};

var getAttribute = function(attribute) {
//Refactor from (attribute in attributes) - ES5 compatible
if (attributes.hasOwnProperty(attribute)) {
return attributes[attribute];
}
};

var setAttribute = function(attribute, value) {
if (attributes.hasOwnProperty(attribute)) {
attributes[attribute] = value;
}
};

return {
getAttribute: getAttribute,
setAttribute: setAttribute
};
};