2016-07-08 8 views
1

과제의이 부분에 문제가 있습니다. 대부분의 코드가 있지만 마지막 부분을 구현하는 데 문제가 있습니다.자바 스크립트에서 개체 출력 표시

3 개월 동안 체크 아웃 및 체크 인을 시뮬레이트하는 루프를 작성하십시오. 매일 카탈로그와 모든 사람들을 후원합니다. 후원자가 현재 도서를 체크 아웃 한 경우 체크 인하십시오. 체크 아웃되지 않은 경우 후원자 읽기 방법을 통해 도서의 후원자 목록에 추가하십시오. 책이 기한이 지난 경우 수혜자에게 $ 5.00의 벌금을 부과합니다. 3 개월의 기간이 끝나면 각 후원자, 그들이 현재 체크 아웃 한 책 및 그들이 가질 수있는 벌금을 표시하십시오.

//Author class 
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("Neil", "Armstrong"); 
authors[1] = new Author("Hard", "Popeye"); 

var mybooks = [] 
mybooks[0] = new Book('gravity', true, new Date(2000, 5, 20), new Date(), 10, authors); 
mybooks[1] = new Book('munro', true, new Date(2000, 5, 20), new Date(), 11, authors); 
mybooks[2] = new Book('kohli', true, new Date(2000, 5, 20), new Date(), 12, authors); 
mybooks[3] = new Book('sachin', true, new Date(2000, 5, 20), new Date(), 13, authors); 
mybooks[4] = new Book('sehwag', true, new Date(2000, 5, 20), new Date(), 14, authors); 

var patrons = [] 
patrons[0] = new Patron('master', 'jumbo', 1, mybooks, 0.00); 
patrons[1] = new Patron('kyle', 'munro', 1, mybooks, 0.00); 
patrons[2] = new Patron('master', 'jumbo2', 1, mybooks, 0.00); 
patrons[3] = new Patron('master', 'jumbo3', 1, mybooks, 0.00); 
patrons[4] = new Patron('master', 'jumbo4', 1, mybooks, 0.00); 

var j = 0; 
while (j < patrons.length) { 
    var books = patrons[j].booksOut; 
    var fine = patrons[j].fine; 
    for (var i = 0; i < books.length; i++) { 
    if (books[i].isOverdue()) { 
     fine = fine + 5.00; 
    } 
    } 
    patrons[j].fine = fine; 
    console.log(patrons[0]); 
} 

문제가 무한히 반복해서 동일한 객체를 출력하고있다 : 여기

는 지금까지있는 코드이다. 왜 그런 일이 일어나고 어떻게 해결할 수 있습니까?

+0

그래서 정확히 문제가 무엇이며 어떻게 당신이 그것을 시도하고 해결하기 위해 짓을 한거야? –

+0

각 후원자의 이름, 체크 아웃 한 도서 및 벌금을 표시하려면 출력물 만 있으면되지만 올바르게 작동하게 만들 수는 없습니다. – dungo

+0

수업 시간에 배운 것을 바탕으로 어떻게 출력합니까? 'console.log'? 'document.write'? 아마도 요소를 쿼리하고 텍스트 내용을 업데이트하는 것입니다. 출력물은 여러 형식으로 제공되며, 모두 당신과 당신의 선생님이 어떻게 보여지기를 원하는지에 달려 있습니다. –

답변

2

나는 코드를 수정하여 출력을 생성하는 방법을 보여줍니다. 그것이 이므로 할당 문제 나는 그것을 완전히 해결할 의사가 없습니다. 예상 출력을 얻으려면 수정하십시오. 문제가 더 있으시면 알려주세요.

//Author class 
 
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("Neil","Armstrong"); 
 
authors[1] = new Author("Hard","Popeye"); 
 

 
var mybooks = [] 
 
mybooks[0] = new Book('gravity',true,new Date(2000,5,20), new Date(), 10,authors); 
 
mybooks[1] = new Book('munro',true,new Date(2000,5,20), new Date(), 11,authors); 
 
mybooks[2] = new Book('kohli',true,new Date(2000,5,20), new Date(), 12,authors); 
 
mybooks[3] = new Book('sachin',true,new Date(2000,5,20), new Date(), 13,authors); 
 
mybooks[4] = new Book('sehwag',true,new Date(2000,5,20), new Date(), 14,authors); 
 

 
var patrons = [] 
 
patrons[0] = new Patron('master','jumbo',1,mybooks,0.00); 
 
patrons[1] = new Patron('kyle','munro',1,mybooks,0.00); 
 
patrons[2] = new Patron('master','jumbo2',1,mybooks,0.00); 
 
patrons[3] = new Patron('master','jumbo3',1,mybooks,0.00); 
 
patrons[4] = new Patron('master','jumbo4',1,mybooks,0.00); 
 

 
var j=0; 
 
while(j < patrons.length){ 
 
    var books = patrons[j].booksOut; 
 
    var fine = patrons[j].fine; 
 
    for(var i=0;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); 
 
}

관련 문제