2017-04-22 141 views
0

我想让我的嵌套循环执行检查/检出模拟。我有我的功能设置和我的循环结束,但它总是给我相同的输出。每个赞助人都拥有相同数量的书籍和巨大的罚款,而不是像checkOut和isOverdue函数那样完全随机化。循环不能正确执行功能操作

我该如何使循环使用从函数中随机生成的日期,并为每位顾客提供不同数量的罚款和不同的书籍? Instructions for the project.

我的代码:

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

 
    function randInRange(start, end) { 
 
     return Math.floor(Math.random() * (end - start + 1)) + start; 
 
    } 
 

 
    Book.prototype.checkOut = function() { 
 
     this.Available = false; 
 
     var dateChk = new Date(); 
 
     var randDay = randInRange(1, 25); 
 
     dateChk.setDate(dateChk.getDate() - randDay); 
 
     this.checkoutDate = dateChk; 
 
    }; 
 

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

+0

请缩小你的问题 –

+0

提示:书随机数具有的任何原始阵列的总长度无关 – charlietfl

+0

@charlietfl随机数为每个顾客都有不同的书检查而不是数字本身。 –

回答

1

我不得不作出一些改变你的例子因此它的实际工作。

要知道一个非常重要的事情是,当您调用对象的方法时,必须在括号后面加上括号:不是catalog[j].checkOut而是catalog[j].checkOut()

您可以检查我的更改以及脚本现在的工作方式。有了这个,你可以制定后续问题。

希望这会有所帮助。

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

 
    function randInRange(start, end) { 
 
     return Math.floor(Math.random() * (end - start + 1)) + start; 
 
    } 
 

 
    Book.prototype.checkOut = function() { 
 
     this.Available = false; 
 
     var dateChk = new Date(); 
 
     var randDay = randInRange(1, 25); 
 
     dateChk.setDate(dateChk.getDate() - randDay); 
 
     this.checkoutDate = dateChk; 
 
    }; 
 

 
    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; 
 
    }; 
 
    
 
    // Changed the read method 
 
    Patron.prototype.read = function(book) { 
 
     this.booksOut[book.callNumber] = book; 
 
    } 
 

 
    // Changed the return method 
 
    Patron.prototype.return = function(book) { 
 
     delete(this.booksOut[book.callNumber]); 
 
    } 
 

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

 
    // Changed how Patrons are initialised. Instead of passing the full 
 
    // catalog for booksRead an empty object is passed. 
 
    var patrons = []; 
 
    patrons[0] = new Patron('Pat1', 'Wat', 1, {}, 0.00); 
 
    patrons[1] = new Patron('Pat2', 'Wot', 1, {}, 0.00); 
 
    patrons[2] = new Patron('Pat3', 'Wit', 1, {}, 0.00); 
 
    patrons[3] = new Patron('Pat4', 'Wet', 1, {}, 0.00); 
 
    patrons[4] = new Patron('Pat5', 'Wut', 1, {}, 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 < 3; i++) { 
 
     // changed the for loop 
 
     for (var j in catalog) { 
 
      // changed the for loop 
 
      for (var k in patrons) { 
 
       var fine = patrons[k].fine; 
 
       // Changed catalog[k] to catalog[j] 
 
       // moved and changed patrons[k].read() call 
 
       // added the patrons[k].return() call 
 
       if (catalog[j].Available) { 
 
        catalog[j].checkOut(); 
 
        patrons[k].read(catalog[j]); 
 
       } else if (patrons[k].booksOut[catalog[j].callNumber]) { 
 
        catalog[j].checkIn(); 
 
        patrons[k].return(catalog[j]); 
 
       } 
 
       if (catalog[j].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 in patrons[i].booksOut) { 
 
      console.log(patrons[i].booksOut[j].title); 
 
     } 
 
     console.log(patrons[i].firstName + " has fine amount: $" + patrons[i].fine); 
 
    }

编辑

答案的评论。

这是您如何设置for循环的结果。第一天,所有的书都去了第一位顾客,第二天他们去了第二位参加者等等。 1

  1. 日,书1,赞助1:书可用,并且去赞助1.
  2. 1天,第1册,守护神2:书是不可用。
  3. ... 1
  4. 日,第2册,赞助1:书可用,并且去赞助1.
  5. 1天,第2册,守护神2:书是不可用。
  6. ...
  7. 第2天,第1册,顾客1:没有图书,图书被退回。
  8. 第2天,第1册,顾客2:本书可用,并转到顾客2.
  9. 第2天,第1册,顾客3:书不可用。
  10. ...
+0

为什么第三位顾客总是得到所有书籍? –

1

的说明并没有说洗牌书或老主顾,在模拟过程中,从而你希望你的结果也不会为随机。此外,还可以循环使用书籍和赞助人,我们可以使用标准forEach和常规for循环,使代码更容易遵循。我使用lodash _.shuffle在每个循环中随机化书籍和顾客以获得更好的结果。我们也可以隐藏我们的老主顾返回原型中的精细逻辑,最终通过一个简单的模拟。

for (var i = 0; i < 90; i++) { 
    _.shuffle(books).forEach(function(book) { 
    _.shuffle(patrons).forEach(function(patron) { 
     if (patron.hasBookCheckedOut(book)) { 
     return patron.return(book); 
     } 

     if (book.available) { 
     patron.read(book); 
     }  
    }); 
    }); 
}; 

工作实例http://jsbin.com/teyipak/1/edit?js,console