gpt4 book ai didi

Javascript - 循环在执行模拟时无法正常工作

转载 作者:行者123 更新时间:2023-11-30 15:22:18 25 4
gpt4 key购买 nike

我正在尝试使用原型(prototype)、对象和嵌套循环运行图书馆 checkin / checkout 系统的模拟。我在将伪代码正确集成到循环本身时遇到了问题,希望得到任何帮助。

//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 i = 0; i < 90; i++) {
for (var j = 0; j < catalog.length; j++) {
for (var k = 0; k < patrons.length; i++) {
var fine = patrons[k].fine;
if (catalog[k].Available) {
catalog[k].checkOut;
} else {
catalog[k].checkIn;
patrons[k].read;
}
if (catalog[k].isOverdue) {
fine = fine + 5.00;
}
}
}
patrons[i].fine = fine;
}

全部代码

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.checkIn = function() {
this.Available = true;
};

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.return = 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);

//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 i = 0; i < 90; i++) {
for (var j = 0; j < catalog.length; j++) {
for (var k = 0; k < patrons.length; i++) {
var fine = patrons[k].fine;
if (catalog[k].Available) {
catalog[k].checkOut;
} else {
catalog[k].checkIn;
patrons[k].read;
}
if (catalog[k].isOverdue) {
fine = fine + 5.00;
}
}
}
patrons[i].fine = fine;
}

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);
}

我正在尝试编写一个循环来模拟 3 个月的 checkout 和 checkin 。每天我都会遍历目录,以及顾客数组中的每个人。如果顾客当前已借出这本书,那么我会将其登记。如果没有借出,那么我会通过顾客阅读方法将其添加到顾客的图书列表中。如果这本书逾期,我会向归还它的赞助人加 5.00 美元的罚款。在 3 个月的期限结束时,我必须显示每位顾客、他们目前借出的书籍以及他们可能收到的任何罚款。

最佳答案

你的循环中有拼写错误(对于 var k ... i++)。查看代码片段

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.checkIn = function() {
this.Available = true;
};

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.return = 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);

//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 i = 0; i < 90; i++) {
for (var j = 0; j < catalog.length; j++) {
for (var k = 0; k < patrons.length; k++) {
var fine = patrons[k].fine;
if (catalog[k].Available) {
catalog[k].checkOut;
} else {
catalog[k].checkIn;
patrons[k].read;
}
if (catalog[k].isOverdue) {
fine = fine + 5.00;
}
patrons[k].fine = fine;
}
}

}

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);
}

关于Javascript - 循环在执行模拟时无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43552259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com