【问题标题】:Display Object Output in Javascript在 Javascript 中显示对象输出
【发布时间】:2016-07-08 16:19:44
【问题描述】:

我在完成这部分作业时遇到了问题。我已经编写了大部分代码,但在实现最后一部分时遇到了麻烦:

编写一个循环来模拟 3 个月的结帐和签入。每天遍历目录,以及顾客数组中的每个人。如果读者当前已将图书签出,则将其签入。如果尚未签出,则通过读者阅读方法将其添加到图书的读者列表中。如果图书过期,则向归还图书的顾客处以 5.00 美元的罚款。在 3 个月期限结束时,展示每位顾客、他们当前已借阅的书籍以及他们可能受到的任何罚款。

这是我目前的代码:

//Author class
var Author = function(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
};

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;
};
Book.prototype.checkOut = function() {
  this.Available = false;
  var temp = new Date(1000000000);
  var d = new Date() - temp;
  var res = new Date(d);
  this.checkoutDate = res;
};
Book.prototype.isOverdue = function() {
  //Get 1 day in milliseconds
  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;
};

var Patron = function(firstName, lastName, libraryCardNumber, booksOut, fine) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.libraryCardNumber = libraryCardNumber;
  this.booksOut = booksOut;
  this.fine = fine;
};
Patron.prototype.read = function(book) {
  this.booksOut.add(book);
}
Patron.prototype.read = function(book) {
  this.booksOut.remove(this.booksOut.length);
}
//creating author objects
var authors = []
authors[0] = new Author("Neil", "Armstrong");
authors[1] = new Author("Hard", "Popeye");

var mybooks = []
mybooks[0] = new Book('gravity', true, new Date(2000, 5, 20), new Date(), 10, authors);
mybooks[1] = new Book('munro', true, new Date(2000, 5, 20), new Date(), 11, authors);
mybooks[2] = new Book('kohli', true, new Date(2000, 5, 20), new Date(), 12, authors);
mybooks[3] = new Book('sachin', true, new Date(2000, 5, 20), new Date(), 13, authors);
mybooks[4] = new Book('sehwag', true, new Date(2000, 5, 20), new Date(), 14, authors);

var patrons = []
patrons[0] = new Patron('master', 'jumbo', 1, mybooks, 0.00);
patrons[1] = new Patron('kyle', 'munro', 1, mybooks, 0.00);
patrons[2] = new Patron('master', 'jumbo2', 1, mybooks, 0.00);
patrons[3] = new Patron('master', 'jumbo3', 1, mybooks, 0.00);
patrons[4] = new Patron('master', 'jumbo4', 1, mybooks, 0.00);

var j = 0;
while (j < patrons.length) {
  var books = patrons[j].booksOut;
  var fine = patrons[j].fine;
  for (var i = 0; i < books.length; i++) {
    if (books[i].isOverdue()) {
      fine = fine + 5.00;
    }
  }
  patrons[j].fine = fine;
  console.log(patrons[0]);
}

问题是它无限地一遍又一遍地输出同一个对象。为什么会发生这种情况,我该如何解决?

【问题讨论】:

  • 那么到底是什么问题,你做了什么来尝试解决它?
  • 我只需要输出显示每个顾客的姓名、他们已签出的书籍和罚款,但我无法让它正常工作。
  • 根据你在课堂上学到的东西,你是如何输出东西的? console.log? document.write?也许查询一个元素并更新它的文本内容。输出有多种格式,这完全取决于您和您的老师希望它如何显示。
  • console.log 我知道如何输出东西,只是当我输出“Patron”时它没有正确输出
  • 您没有在 while(j

标签: javascript object output


【解决方案1】:

我已经调整了您的代码并向您展示了一种生成输出的方法。因为它是分配问题,我无意完全解决它。修改它以获得预期的输出。如果您还有其他问题,请告诉我们。

//Author class
var Author = function(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
};

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

Book.prototype.checkOut = function(){
  this.Available = false;
  var temp = new Date(1000000000);
  var d = new Date()-temp;
  var res = new Date(d);
  this.checkoutDate = res;
};

Book.prototype.isOverdue = function(){
  //Get 1 day in milliseconds
  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;
};

var Patron = function(firstName, lastName, libraryCardNumber, booksOut, fine) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.libraryCardNumber = libraryCardNumber;
  this.booksOut = booksOut;
  this.fine = fine;
};

Patron.prototype.read = function(book){
  this.booksOut.add(book);
}

Patron.prototype.read = function(book){
  this.booksOut.remove(this.booksOut.length);
}

//creating author objects
var authors = []
authors[0] = new Author("Neil","Armstrong");
authors[1] = new Author("Hard","Popeye");

var mybooks = []
mybooks[0] = new Book('gravity',true,new Date(2000,5,20), new Date(), 10,authors);
mybooks[1] = new Book('munro',true,new Date(2000,5,20), new Date(), 11,authors);
mybooks[2] = new Book('kohli',true,new Date(2000,5,20), new Date(), 12,authors);
mybooks[3] = new Book('sachin',true,new Date(2000,5,20), new Date(), 13,authors);
mybooks[4] = new Book('sehwag',true,new Date(2000,5,20), new Date(), 14,authors);

var patrons = []
patrons[0] = new Patron('master','jumbo',1,mybooks,0.00);
patrons[1] = new Patron('kyle','munro',1,mybooks,0.00);
patrons[2] = new Patron('master','jumbo2',1,mybooks,0.00);
patrons[3] = new Patron('master','jumbo3',1,mybooks,0.00);
patrons[4] = new Patron('master','jumbo4',1,mybooks,0.00);

var j=0;
while(j < patrons.length){
  var books = patrons[j].booksOut;
  var fine = patrons[j].fine;
  for(var i=0;i<books.length;i++){
    if(books[i].isOverdue()){
      fine = fine + 5.00;
    }
  }
  patrons[j].fine = fine;
  j++;
}

for(i=0; i < patrons.length;i++){
  console.log(patrons[i].firstName+" has taken 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 = "+patrons[i].fine);
}

【讨论】:

    猜你喜欢
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多