0

I am trying to create a bookshelf with various books. However, when I attempt to make a new bookshelf for every 4 books, I get an error. Possibly an infinite for loop? What is going wrong? (Khan Academy Program)

An array of books.

var book = [
    {
        title: "The Giver",
        stars: 4,
        author: "Lois Lowry",//2.Author property to each book                                  added #1
        color: color(0, 120, 42),//3. Property that stores                                          color
        recommended: true
    },
    {
        title: "NWT of the Holy Scriptures",
        stars: 5,
        author: "Jehovah",//2.Author property... #2
        color: color(204, 204, 204),//3. Property that stores                                          color
        recommended: true
    },
    {
        title: "The Cay",
        stars: 4,
        author: "Theodore Taylor",//2.Author property... #3
        color: color(80, 84, 209),//3. Property that stores                                          color
        recommended: true
    },
    {
        title: "The Golden Compass",
        stars: 5,
        author: "Philip Pullman",//2.Author property... #4
        color: color(97, 55, 186),//3. Property that stores                                          color
        recommended: true
    },
];

Draw bookshelves and books

for(var x = 0; x < book.length; x++){
    //Draw books
    fill(book[x].color);
    rect(5 + 100 * x, 20, 90, 100);
    fill(0, 0, 0);
    text(book[x].title, 15 + 100 * x, 29, 70, 100);
    text(book[x].author,35 + 100 * x, 76, 70, 100);

    //Draw leaf for recommended books
    if(book[x].recommended === true){
        var leaf = getImage("avatars/leaf-red");
        image(leaf, 10 + 100 * x, 85,25,25);
    }
    //Draw stars for star rating
    for (var i = 0; i < book[x].stars; i++) {
        image(getImage("cute/Star"), 17 + i * 15 + 100 * x, 96         , 15, 25);
    }
    //Draw bookshelf for every 4 books
    for(var y = book.length;y >= 0;y - 4){
        // draw shelf
        fill(87, 10, 0);
        rect(0, 120 + 100 * y, width, 10);
    }/// <------ infinite loop?
}
4
  • A code snippet would help. Commented Apr 4, 2015 at 6:36
  • What is a code snippet? Commented Apr 4, 2015 at 6:37
  • 4
    You should assign the reduced value back to y, like this y -= 4 Commented Apr 4, 2015 at 6:37
  • Please post in answer section so I can accept your answer :) Thank you! Commented Apr 4, 2015 at 6:43

1 Answer 1

1
for(var y = book.length;y >= 0;y - 4){

...is not actually mutating the value of y. Change it to:

for( var y = book.length; y >= 0; y -= 4 ) {
Sign up to request clarification or add additional context in comments.

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.