为什么我使用javascript获得无限循环?

时间:2015-04-04 06:34:20

标签: javascript

我正在尝试用各种书籍创建一个书架。但是,当我试图为每4本书制作一个新的书架时,我收到了一个错误。可能是一个无限循环?出了什么问题? (可汗学院计划)

一系列书籍。

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

画书架和书籍

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

1 个答案:

答案 0 :(得分:1)

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

...实际上并没有改变y的值。将其更改为:

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