Insert javascript image

-1

Good, I want to insert an image according to the amount of times in the property of an object. It's easier to explain with an example:

I have this object:

var book = [
    {
        title: "O Tatuador de Auschwitz",
        stars: 4,
        Author: "Heather Morris",
        Validation: true
    }
]

and in this case I want to insert an image 4 times (according to the Stars property). For this I have created a for loop as follows:

for (var s = 0; s < book.stars ; s++) {
            image(starj,s,100,20,20);
    }

starj is defined with the image that I want to insert

What am I doing wrong?

Thank you

P.S. I'm still a lot of blue whites in it.

    
asked by anonymous 23.08.2018 / 02:23

2 answers

0

The Array index and the field you want to get book[0]["stars"] are missing:

var s;
var book = [
   {
       title: "O Tatuador de Auschwitz",
       stars: 4,
       Author: "Heather Morris",
       Validation: true
   }
];
for(s = 0; s < book[0]["stars"]; s++){
     var img = document.createElement("IMG");
     img.src = "https://i.imgur.com/F543MoZ.png";
     img.style.width = "30px";
     img.style.height = "30px";
     document.getElementById('imagens').appendChild(img);
}
<!DOCTYPE html>
<html>
<head>
   <title>teste</title>
</head>
<body>
   <div id="imagens"></div>
</body>
</html>
    
23.08.2018 / 05:50
0

Thank you Gabriel, I already found out what was missing. So I finished the code this way:

for (var i = 0; i < book.length; i++){
fill(random(0,250), random(50, 175) , 219);
rect(10+i*120, 20, 90, 100);
fill(0, 0, 0);
text(book[i].title, 15+i*120, 29, 70, 100);
text(book[i].Author, 15+i*120, 82, 70, 100 );
    for (var s = 0; s < book[i].stars; s++) {
        image(starj,(i*120)+(s*15),160,30,30);
    }

In this way, the stars image is inserted horizontally with the number of times present of the Stars property of the Book object.

    
23.08.2018 / 13:54