试图为循环生成一个随机数,但它似乎不起作用。为什么是这样?

时间:2017-03-01 00:52:11

标签: javascript

所以我试图制作一个循环,但我需要一个随机数来实际让它起作用。

var j=0;
while(j < patrons.length){
  var books = patrons[j].booksOut;
  var fine = patrons[j].fine;
  var randomnumber = Math.floor(Math.random() * (20 - 5 + 1)) + 1; //the random number
  for(var i=randomnumber;i<books.length;i++){ //me trying to replace i with the random number
    if(books[i].isOverdue()){
      fine = fine + 5.00;
    }
  }
  patrons[j].fine = fine;
  j++;
}

问题是,当我尝试用随机数替换我的for循环中的i时,它似乎无法正常工作。谁知道为什么?

我的完整代码:

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("Edgar","A. Poe");
authors[1] = new Author("George","Orwell");

var mybooks = []
mybooks[0] = new Book('Animal Farm',true,new Date(2000,5,20), new Date(), 10,authors);
mybooks[1] = new Book('1984',true,new Date(2000,5,20), new Date(), 11,authors);
mybooks[2] = new Book('A Tale of Two Cities',true,new Date(2000,5,20), new Date(), 12,authors);
mybooks[3] = new Book('The Raven',true,new Date(2000,5,20), new Date(), 13,authors);
mybooks[4] = new Book('Forgotten Lore',true,new Date(2000,5,20), new Date(), 14,authors);

var patrons = []
patrons[0] = new Patron('Patrick','fill1',1,mybooks,0.00);
patrons[1] = new Patron('Lira','fill2',1,mybooks,0.00);
patrons[2] = new Patron('May','fill3',1,mybooks,0.00);
patrons[3] = new Patron('Kyle','fill3',1,mybooks,0.00);
patrons[4] = new Patron('Bob','fill4',1,mybooks,0.00);

var j=0;
while(j < patrons.length){
  var books = patrons[j].booksOut;
  var fine = patrons[j].fine;
  var randomnumber = Math.floor(Math.random() * (20 - 5 + 1)) + 1;
  for(var i=randomnumber;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);
}

2 个答案:

答案 0 :(得分:0)

如果没有顾客的样本值,我能提供的最好的就是使用books.length作为最大值。你为什么要随意挑选它们?您正试图解决更广泛的问题吗?

const isOverdue = () => true;
const patrons = [
  { booksOut: [{ isOverdue }], fine: 1.0 },
  { booksOut: [{ isOverdue }], fine: 2.0 },
  { booksOut: [{ isOverdue }], fine: 3.0 },
];
let j = 0;
while(j < patrons.length) {
  const books = patrons[j].booksOut;
  let fine = patrons[j].fine;
  const randomnumber = Math.floor(Math.random() * books.length);

  for(let i = randomnumber; i < books.length; i++) {
    if(books[i].isOverdue()) {
      fine += 5.00;
    }
  }
  patrons[j].fine = fine;
  j += 1;
}

console.log(JSON.stringify(patrons, null, 2));

根据评论编辑:

Book.prototype.isOverdue = function() {
  return [true, false][Math.floor(Math.random() * 2)]
}

如上所述会使isOverdue()随机回答是或否。

答案 1 :(得分:0)

在您提供的示例中,您使用new Date()设置每本书的结帐日期,并将其设置为执行时间。

因此,当您致电isOverdue()时,它始终会检查今天减去现在是否> = 14天,这是永远不会的,所以isOverdue()将始终返回false。