2017-07-18 136 views
0

我想繪製一個書架使用書籍和嵌套循環的書架。 for循環爲本書繪製書架,書籍和標題以及明星(評級)。但是,在嘗試訪問星星變量時,我收到了「明星未定義錯誤」。任何幫助深表感謝。無法讀取屬性,變量undefined-Javascript

var appsPerShelf = 4; 

var apps = [ 
{title: "My House", author:"Claire", stars: 4}, 
{title: "My Animal":, author: "Claire", stars: 5}, 
{title: "Funny Face", author: "Claire", stars: 2}, 
{title: "My Inital", author: "Claire", stars:3}, 
{title: "Dancing Animals", author:"Claire", stars: 1}, 
{title: "Racing Animals", author:"Claire", stars: 5}, 
{title: "Resize Animal", author: "Claire", stars: 3}, 
{title: "App Bookshelf", author: "Claire",stars: 2}, 
{title: "Ball Follow App", author:"Claire", stars: 3}, 
{title: "Red Soz Quiz App", author:"Claire", stars: 5}, 
{title: "Zoo App", author:"Claire", stars: 4}, 
{title: "Dry Animal App", author:"Claire", stars: 5}, 
{title: "Dancing Animals Fun", author:"Claire", stars: 3}, 
{title: "Racing Animals Fun", author:"Claire", stars: 1}, 
{title: "Red Sox Quiz", author: "Claire", stars: 3} 
]; 




//shelf drawer 
for(var i=0; i<14%appsPerShelf; i++){ 
fill(173, 117, 33); 
rect(0, 20+(20*i), width, 10); 

//app drawer 
for(var j=0; j<appsPerShelf; j++){ 
    fill(214, 255, 219); 
    rect(10, 20, 90, 100); 
    fill(0, 0, 0); 
    text(apps[j].title, 15+(20*i), 19+(20*i), 70, 100); 

//star drawer 
if(apps[i*appsPerShelf+j].stars){ 
for(var k=0; apps[i*appsPerShelf+j].stars; k++){ 
    var img = getImage("cute/Star"); 
    }} 
} 
} 
+0

你的意思是'不能讀取undefined'的財產 '明星'? – Marty

+0

是的!我不會如果這是一個問題的任何不同 –

+0

您的問題表明'星星未定義的錯誤',而實際的問題是,你試圖訪問'星星'的東西是'undefined'('apps [i * appsPersShelf + j]'解析爲'undefined',所以沒有任何屬性)。 – Marty

回答

0

你行更改此

for (var k = 0; k < apps[i * appsPerShelf + j].stars; k++) 

例如

var appsPerShelf = 4; 
 

 
var apps = [{ 
 
    title: "My House", 
 
    author: "Claire", 
 
    stars: 4 
 
    }, 
 
    { 
 
    title: "My Animal", 
 
    author: "Claire", 
 
    stars: 5 
 
    }, 
 
    { 
 
    title: "Funny Face", 
 
    author: "Claire", 
 
    stars: 2 
 
    }, 
 
    { 
 
    title: "My Inital", 
 
    author: "Claire", 
 
    stars: 3 
 
    }, 
 
    { 
 
    title: "Dancing Animals", 
 
    author: "Claire", 
 
    stars: 1 
 
    }, 
 
    { 
 
    title: "Racing Animals", 
 
    author: "Claire", 
 
    stars: 5 
 
    }, 
 
    { 
 
    title: "Resize Animal", 
 
    author: "Claire", 
 
    stars: 3 
 
    }, 
 
    { 
 
    title: "App Bookshelf", 
 
    author: "Claire", 
 
    stars: 2 
 
    }, 
 
    { 
 
    title: "Ball Follow App", 
 
    author: "Claire", 
 
    stars: 3 
 
    }, 
 
    { 
 
    title: "Red Soz Quiz App", 
 
    author: "Claire", 
 
    stars: 5 
 
    }, 
 
    { 
 
    title: "Zoo App", 
 
    author: "Claire", 
 
    stars: 4 
 
    }, 
 
    { 
 
    title: "Dry Animal App", 
 
    author: "Claire", 
 
    stars: 5 
 
    }, 
 
    { 
 
    title: "Dancing Animals Fun", 
 
    author: "Claire", 
 
    stars: 3 
 
    }, 
 
    { 
 
    title: "Racing Animals Fun", 
 
    author: "Claire", 
 
    stars: 1 
 
    }, 
 
    { 
 
    title: "Red Sox Quiz", 
 
    author: "Claire", 
 
    stars: 3 
 
    } 
 
]; 
 

 
//shelf drawer 
 
for (var i = 0; i < 14 % appsPerShelf; i++) { 
 

 
    //app drawer 
 
    for (var j = 0; j < appsPerShelf; j++) { 
 
    console.log(apps[j].title); 
 
    //star drawer 
 
    var stars = ""; 
 
    for (var k = 0; k < apps[i * appsPerShelf + j].stars; k++) { 
 
     stars += "*"; 
 
    } 
 
    console.log(stars); 
 
    } 
 
}