2015-04-04 72 views
0

我正嘗試用各種書籍創建一個書架。但是,當我嘗試爲每4本書創建一個新書架時,出現錯誤。可能是一個無限循環?出了什麼問題? (汗學院計劃)爲什麼我使用javascript獲取無限循環?

一系列的書籍。

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

代碼片段將有所幫助。 – OddDev 2015-04-04 06:36:20

+0

什麼是代碼片段? – 2015-04-04 06:37:37

+4

你應該減少的價值回到'Y',就像這樣'Y - = 4' – thefourtheye 2015-04-04 06:37:38

回答

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

...實際上並沒有突變的y值。將其更改爲:

for(var y = book.length; y >= 0; y -= 4) { 
相關問題