0
我試圖運行庫檢查/結帳系統的模擬。我製作了所有的對象,但在執行模擬本身的循環中遇到了麻煩。我的代碼:麻煩整合嵌套的和如果循環在Javascript中
的僞代碼:
//while loop or for loop for 90 days
//For loop over catalog
//forloop over patrons
//Check if available , if so check book out
//If not available check book back in
//check checking back in check to see if book is overdue and if so add a fine
//When down loop over patrons to see their fees
我在這個循環
for (var j = 0; j < 90; j++) {
for (var i = 0; i < catalog.length; i++) {
for (var k = 0; k < patrons.length; k++) {
if (books[k].Available = true) {
books[k].checkOut = true; }
else {
books[k].Available = true;
}
if (books[k].isOverdue = true) {
fine = fine + 5.00;
}
}
}
patrons[j].fine = fine;
j++;
}
所有代碼
var Book = function(title, Available, publicationDate, checkoutDate, callNumber, Authors) {
this.title = title;
this.Available = Available;
this.publicationDate = publicationDate;
this.checkoutDate = checkoutDate;
this.callNumber = callNumber;
this.Authors = Authors;
};
var Author = function(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
};
var Patron = function(firstName, lastName, libCardNum, booksOut, fine) {
this.firstName = firstName;
this.lastName = lastName;
this.libCardNum = libCardNum;
this.booksOut = booksOut;
this.fine = fine;
};
Book.prototype.checkOut = function() {
this.Available = false;
var temp = new Date(1000000000);
var date = new Date() - temp;
var res = new Date(date);
this.checkoutDate = res;
};
Book.prototype.isOverdue = function() {
var singleDay = 1000 * 60 * 60 * 24;
var todayDate = new Date().getTime();
var difference = todayDate - this.checkoutDate.getTime();
if (Math.round(difference/singleDay) >= 14) {
return true;
}
return false;
};
Patron.prototype.read = function(book) {
this.booksOut.add(book);
}
Patron.prototype.read = function(book) {
this.booksOut.remove(this.booksOut.length);
}
var authors = [];
authors[0] = new Author("Auth", "One");
authors[1] = new Author("AutL", "Two");
var catalog = [];
catalog[0] = new Book('Bk1', true, new Date(2001, 1, 21), new Date(), 123456, authors);
catalog[1] = new Book('Bk2', true, new Date(2002, 2, 22), new Date(), 987656, authors);
catalog[2] = new Book('Bk3', true, new Date(2003, 3, 23), new Date(), 092673, authors);
catalog[3] = new Book('Bk4', true, new Date(2004, 4, 24), new Date(), 658342, authors);
catalog[4] = new Book('Bk5', true, new Date(2005, 5, 25), new Date(), 345678, authors);
var patrons = [];
patrons[0] = new Patron('Pat1', 'Wat', 1, catalog, 0.00);
patrons[1] = new Patron('Pat2', 'Wot', 1, catalog, 0.00);
patrons[2] = new Patron('Pat3', 'Wit', 1, catalog, 0.00);
patrons[3] = new Patron('Pat4', 'Wet', 1, catalog, 0.00);
patrons[4] = new Patron('Pat5', 'Wut', 1, catalog, 0.00);
var j = 0;
//while loop or for loop for 90 days
//For loop over catalog
//forloop over patrons
//Check if available , if so check book out
//If not available check book back in
//check checking back in check to see if book is overdue and if so add a fine
//When down loop over patrons to see their fees
for (var j = 0; j < 90; j++) {
for (var i = 0; i < catalog.length; i++) {
for (var i = 0; i < patrons.length; i++) {
if (books[i].Available = true) {
books[i].checkOut = true; }
else {
books[i].Available = true;
}
if (books[i].isOverdue = true) {
fine = fine + 5.00;
}
}
}
patrons[j].fine = fine;
j++;
}
for (i = 0; i < patrons.length; i++) {
console.log(patrons[i].firstName + " has checked out the following books:");
for (j = 0; j < patrons[i].booksOut.length; j++) {
console.log(patrons[i].booksOut[j].title);
}
console.log(patrons[i].firstName + " has fine amount: $" + patrons[i].fine);
}
您使用的是相同的循環變量有兩個嵌套循環:不會工作... – trincot
@trincot編輯它,但仍然給出相同的輸出。 ReferenceError說:書籍沒有定義 –
你從來沒有定義過「書」,所以你期望什麼?你有很多混合:'顧客[j]''而'k'迭代'顧客'索引和'j'將要90?你沒有那麼多的「老主顧」。而'catalog'數組有書,但是你把它作爲'books'引用,但是不能和遍歷'catalog'索引的索引.... hmmm。也許小睡一下,重新開始? ;-) – trincot