2017-03-01 24 views
0

所以我試圖做一個循環,但我需要一個隨機數來實際使它工作。試圖爲循環生成一個隨機數,但它似乎沒有工作。爲什麼是這樣?

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

隨機數不考慮書的數量。如果生成的數字大於或等於'books.length',會發生什麼情況? –

+0

好吧,隨機數將從1到17(爲什麼這個範圍?)...書籍數組中有多少項? –

+0

你得到什麼錯誤?嘗試記錄您正在生成的隨機數並檢查它是否不大於'books.length' –

回答

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()隨機回答yes或no。

+0

也許這是一個經典的XY問題。我試圖生成一個隨機數,以便它觸發逾期的功能,並使用它來使它成爲用戶有罰款。 –

+0

可能希望將隨機數字放在'Book.prototype.isOverDue()'中,以便它隨機決定何時過期。 –

0

在您提供的示例中,您正在使用new Date()來設置每本書的結帳日期,該日期將其設置爲執行時間。

因此,當您打電話給isOverdue()時,它總是檢查今天減去現在是否> = 14天,這永遠不會發生,因此isOverdue()將始終返回false。

相關問題