2017-04-21 36 views
1

我试图运行原型,对象和嵌套循环的库检入/出系统模拟。我无法正确地将伪代码集成到循环中,并希望得到任何帮助。Javascript - 循环在执行模拟时无法正常工作

//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; i++) { 
       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[i].fine = fine; 
    } 

所有

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

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

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; i++) { 
      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[i].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); 
} 

我试图写一个循环,模拟检出,并且签入的3个月内的代码。我每天迭代目录,以及顾客数组中的每个人。如果该顾客目前已经将该书签出,那么我将它签入。如果它未被签出,那么我通过顾客阅读方法将其添加到顾客列表中。如果这本书已经逾期,那么我会向返还它的顾客追加5美元的罚款。在3个月期间结束时,我必须展示每位赞助人,他们目前已经签出的书籍以及他们可能拥有的任何罚款。

+1

您是否知道您正在使用你在初始化循环之外的'k'? –

+0

@MikeC对不起,这是一个拼写错误,我只是测试,并忘记回到它为每循环。尽管如此,问题仍然存在。 –

+0

行'patrons [i] .fine = fine;'可能会产生错误,因为您正在使用运行90次循环的迭代器引用您的赞助人数组(例如长度为5)。可能会产生超出范围错误的索引。 – Ken

回答

1

你在你的循环中有错字错误(对于var k ... i ++)。看到的片段

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

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

 
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

可能这是要去帮助

请记住,他们要求3个月期或90天左右。我们不能只重复90次。最终你会在同一个月结束。举一个例子:如果今天是2017年10月19日,这意味着该计划应计算至2018年1月19日(3个月期)

//***************** library.js *****************// 
 

 

 
/** 
 
\t Create a constructor function for a Book object. The Book object should have the following properties: 
 
\t 
 
\t Title: string 
 
\t Available: Boolean representing whether the book is checked out or not. The initial value should be false. 
 
\t Publication Date: Use a date object 
 
\t Checkout Date: Use a date object 
 
\t Call Number: Make one up 
 
\t Authors: Should be an array of Author objects 
 
**/ 
 
var Book = function(title, available, publicationDate, checkOutDate, callNumber, authors) { 
 

 
\t this.title = title; 
 
\t this.available = available; 
 
\t this.publicationDate = publicationDate; 
 
\t this.checkOutDate = checkOutDate; 
 
\t this.callNumber = callNumber; 
 
\t this.authors = authors; 
 
} 
 

 
/** 
 
\t Create a constructor function for an object called Author. It should have a property for the 
 
\t 
 
\t first name: string 
 
\t last name: string 
 
**/ 
 
var Author = function(firstName, lastName) { 
 

 
\t this.firstName = firstName; 
 
\t this.lastName = lastName; 
 
} 
 

 
/** 
 
\t Create a constructor function for an object called Patron. This represents a person who is allowed to check out books from the library. Give it the following properties: 
 

 
\t Firstname: string 
 
\t Lastname: string 
 
\t Library Card Number (Make one up): string 
 
\t Books Out (make it an array): [] 
 
\t fine (Starts a 0.00): parseFloat(0.00) 
 
**/ 
 
var Patron = function(firstName, lastName, libraryCardNumber, booksOut, fine) { 
 
\t 
 
    this.firstName = firstName; 
 
    this.lastName = lastName; 
 
    this.libraryCardNumber = libraryCardNumber; 
 
    this.booksOut = booksOut; 
 
    this.fine = parseFloat(fine) || 0.00; 
 
} 
 

 
// Methods 
 
/** 
 
Add a function to the Book prototype called "checkOut". The function will change the available property of the book from true to false and set the checkout date. The checkout date should be set to the current date minus some random number of days between 1 and 5. This will allow us to simulate books being overdue. 
 
**/ 
 
Book.prototype.checkOut = function(date) { 
 
    this.available = false; 
 
    
 
    this.checkOutDate = new Date(date.setDate(date.getDate() - Math.floor((Math.random() * 5) + 1))); 
 
} 
 

 
/** 
 
Add a function to the Book prototype called "checkIn". The function will change the available property of the book from false to true. 
 
**/ 
 
Book.prototype.checkIn = function() { 
 
    this.available = true; 
 
} 
 

 
/** 
 
Add a function called isOverdue that checks the current date and the checked out date and if it's greater than 5 days it returns true 
 
**/ 
 
Book.prototype.isOverdue = function(date) { 
 
\t var time = date.getTime(); 
 
\t var checkOutDate = (this.checkOutDate).getTime(); 
 
\t var timeDiff = Math.abs(time - checkOutDate); 
 
\t var dayDiff = Math.ceil(timeDiff/ (1000 * 3600 * 24)); 
 
    
 
\t if (dayDiff > 5) { 
 
\t \t return true; 
 
\t } else { 
 
\t \t return false; 
 
\t } 
 
} 
 

 
/** 
 
Add a function called isCheckedOut that returns true if the book is checked out to someone and false if not. 
 
**/ 
 
Book.prototype.isCheckedOut = function() { 
 
\t if (this.available === true) { 
 
\t \t return false; 
 
\t } else { 
 
\t \t return true; 
 
\t } 
 
} 
 

 
/** 
 
Add a function to the Patron prototype called "read" that adds a book to it's books out property. 
 
**/ 
 
Patron.prototype.read = function(book) { 
 
\t this.booksOut.push(book); 
 
} 
 

 
/** 
 
Add a function to the Patron prototype called "return" that removes a book from it's books out property. 
 
**/ 
 
Patron.prototype.return = function(book) { 
 
\t var booksOut = this.booksOut; 
 
\t booksOut.forEach(function(existingBook, index) { 
 
\t \t if (existingBook.title === book.title) { 
 
\t \t \t booksOut.splice(index, 1); 
 
\t \t } 
 
\t }); 
 
} 
 

 
/** 
 
Create 5 different books from the Book Class and store them in an array called catalog. 
 
**/ 
 
var catalog = []; 
 

 
catalog[0] = new Book('Another Brooklyn', false, new Date(2017, 5, 30), new Date(), '201-360-9955', [new Author('Jacqueline', 'Woodson')]); 
 
catalog[1] = new Book('Sunshine State', false, new Date(2017, 4, 11), new Date(), '888-888-888', [new Author('Sarah', 'Gerard')]); 
 
catalog[2] = new Book('Homegoing', false, new Date(2017, 5, 2), new Date(), '877-990-3321', [new Author('Yaa', 'Gyasi')]); 
 
catalog[3] = new Book('Round Midnight', false, new Date(2015, 11, 22), new Date(), '321-221-2100', [new Author('Laura', 'McBride')]); 
 
catalog[4] = new Book('The Sport of Kings', false, new Date('08/22/2016'), new Date(), '813-289-0007', [new Author('C.E.', 'Morgan')]); 
 

 
/** 
 
Create 5 different patrons from the Patron Class and store them in an array called patrons. 
 
**/ 
 
var patrons = []; 
 

 
patrons[0] = new Patron('Bhaumik', 'Mehta', 'Lib-229781', [catalog[0]], 0.00); 
 
patrons[1] = new Patron('Jeff', 'Reddings', 'Lib-337895', [catalog[1]], 0.00); 
 
patrons[2] = new Patron('Andrew', 'Hansing', 'Lib-227896', [catalog[2]], 0.00); 
 
patrons[3] = new Patron('Dylan', 'Marks', 'Lib-672563', [catalog[3]], 0.00); 
 
patrons[4] = new Patron('Roop', 'Kapur', 'Lib-008936', [catalog[4]], 0.00); 
 

 

 
/** 
 
Write a loop that simulates checkouts and checkins for a 3 month period. Every day iterate over each book in the catalog, and every person in the patrons array(Nested Loops). If the current book is not checked out and the current patron does not have a book already, then add it to the patrons list of books via the patrons read method and check it out by calling the books checkout method. If the book is checked out to the current person, then check if it is overdue, and if so, check it in using the books check in method, and return it using the patrons return method. When checking in, if the book is overdue then add a fine of $5.00 to the patron returning it. At the end of the 3 month period, display each patron, the books they have currently checked out and any fine they may have. 
 
**/ 
 

 
var now = new Date(); 
 
var threeMonthsFromNow = new Date(now.setMonth(now.getMonth() + 3)); 
 
var timeDiff = Math.abs(threeMonthsFromNow.getTime() - new Date().getTime()); 
 
var days = Math.ceil(timeDiff/ (1000 * 3600 * 24)); 
 

 
for (var i = 0; i < days; i ++) { 
 

 
    var date = new Date(new Date().setDate(new Date().getDate() + i)); 
 
    
 
    catalog.forEach(function(item, index) { 
 
    
 
\t  patrons.forEach(function(patron, index) { 
 

 
\t \t  if (item.isCheckedOut() === false) { 
 
    
 
\t \t \t  var booksOut = (patron.booksOut).filter(function(book) { 
 
\t \t \t \t  return book.title === item.title; 
 
\t \t \t  }); 
 
    
 
\t \t \t  if (booksOut.length === 0) { 
 
    
 
\t \t \t \t  patron.read(item); 
 
\t \t \t \t  item.checkOut(date); 
 
\t \t \t  } 
 
    
 
\t \t  } else if (item.isCheckedOut() === true) { 
 
    
 
\t \t \t  var booksOut = (patron.booksOut).filter(function(book) { 
 
\t \t \t \t  return book.title === item.title; 
 
\t \t \t  }); 
 
    
 
\t \t \t  if (booksOut.length > 0) { 
 
    
 
\t \t \t \t  var isOverdue = item.isOverdue(date); 
 
    \t \t \t \t 
 
\t \t \t \t  if (isOverdue === true) { 
 
     
 
\t \t \t \t \t  item.checkIn(); 
 
\t \t \t \t \t  patron.return(item); 
 
\t \t \t \t \t  patron.fine += parseFloat(5.00); 
 
\t \t \t \t  } 
 
\t \t \t  } 
 
\t \t  } 
 
\t  }); 
 
    }); 
 
} 
 

 
/** 
 
Object with the method to execute the record list for patrons 
 
**/ 
 
var records = { 
 
\t 
 
\t patronInfo: function() { 
 
    
 
\t \t patrons.forEach(function(patron, index) { 
 

 
\t \t \t console.log('Patron name: ' + patron.firstName + ' ' + patron.lastName); 
 
\t \t \t console.log('Patron library card number: ' + patron.libraryCardNumber); 
 
\t \t \t console.log('List of book(s) checked out currently by the patron:'); 
 

 
\t \t \t if ((patron.booksOut).length === 0) { 
 

 
\t \t \t \t console.log('NO BOOKS ARE CHECKED OUT CURRENTLY'); 
 

 
\t \t \t } else { 
 

 
\t \t \t \t (patron.booksOut).forEach(function(book, index) { 
 

 
\t \t \t \t \t console.log((index + 1) + '. | ' + book.title + ' | Checked out on: ' + (book.checkOutDate).toDateString()); 
 
\t \t \t \t }); 
 
\t \t \t } 
 

 
\t \t \t console.log('Patron ' + patron.firstName + ' ' + patron.lastName + ' has fine amout(till date): $' + (patron.fine).toFixed(2)); 
 
\t \t \t console.log('-----------------------------------------------------'); 
 
\t \t }); 
 
\t } 
 
}; 
 

 
/** 
 
Method execution 
 
**/ 
 
records.patronInfo();