0

I am trying to run a simulation of a library check in/checkout system. I have all the objects made but am having trouble with the loop that performs the simulation itself. My code:

The pseudo code:

//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

My try at that loop

for (var j = 0; j < 90; j++) {            
    for (var i = 0; i < catalog.length; i++) {
        for (var k = 0; k < patrons.length; k++) {
            if (books[k].Available = true) {
                books[k].checkOut = true; }
                else {
                    books[k].Available = true;
                }
                if (books[k].isOverdue = true) {
                    fine = fine + 5.00;
                }
            }
        }
        patrons[j].fine = fine;
        j++;
    }

All the code

    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.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.read = 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);

var j = 0;

//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 j = 0; j < 90; j++) {            
    for (var i = 0; i < catalog.length; i++) {
        for (var i = 0; i < patrons.length; i++) {
            if (books[i].Available = true) {
                books[i].checkOut = true; }
                else {
                    books[i].Available = true;
                }
                if (books[i].isOverdue = true) {
                    fine = fine + 5.00;
                }
            }
        }
        patrons[j].fine = fine;
        j++;
    }

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);
}
7
  • 1
    You're using the same loop variable for two nested loops: that wont work... Commented Apr 20, 2017 at 21:30
  • @trincot Edited it in, but still gives the same output. Says ReferenceError: books is not defined Commented Apr 20, 2017 at 21:32
  • 3
    You never defined books, so what did you expect? You have a lot mixed up there: patrons[j] while k iterates over patrons indexes and j is going to 90? You don't have that many patrons. And catalog array has books, but you reference it as books, but not with the index that iterates over the catalog indexes .... hmmm. Maybe take a nap and start again? ;-) Commented Apr 20, 2017 at 21:36
  • Why don't you use a database? I don't think this script is very useful when items are hardcoded in javascript. Or is this just for practice? Commented Apr 20, 2017 at 21:41
  • 1
    @trincot I am not the best at loops, they always throw me for a loop. I will try to learn more about them. Appreciate the advice. Commented Apr 20, 2017 at 21:47

2 Answers 2

1

In your code change your duplicated variables names i and don't forget to add var to not make them global:

for (var j = 0; j < 90; j++) {            
    for (var i = 0; i < catalog.length; i++) {
        for (var n = 0; n < patrons.length; n++) {
            if (books[n].Available = true) {
                books[n].checkOut = true; }
                else {
                    books[n].Available = true;
                }
                if (books[n].isOverdue = true) {
                    fine = fine + 5.00;
                }
            }
        }
        patrons[j].fine = fine;
        j++;
    }

for (var i = 0; i < patrons.length; i++) {
    console.log(patrons[i].firstName + " has checked out the following books:");
    for (var 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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

you second and third loops use the same iteration variable (i)

Perhaps use different name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.