所以我試圖做一個循環,但我需要一個隨機數來實際使它工作。試圖爲循環生成一個隨機數,但它似乎沒有工作。爲什麼是這樣?
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循環時,它似乎不能正常工作。有人知道爲什麼
我的完整代碼:
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);
}
隨機數不考慮書的數量。如果生成的數字大於或等於'books.length',會發生什麼情況? –
好吧,隨機數將從1到17(爲什麼這個範圍?)...書籍數組中有多少項? –
你得到什麼錯誤?嘗試記錄您正在生成的隨機數並檢查它是否不大於'books.length' –