Adam Hoxhaj
Adam Hoxhaj

Reputation: 1

How can I use an object and create squares that are different colors

I am trying to just do some simple coding on Khan academy. The program simply creates 4 books, a shelf, author, title, and rating of the book. I ran into a problem where I had objects storing colors when I tried to plug in the color into a fill command it never worked. Also the stars (rating system) is messed up help me with that if you feel like it.

book[{
  bookColor: [255, 0, 0];
} {
  bookColor: [0, 0, 255]
}]
for (var j = 0; j < 4; j++) {
  fill(book[j].colorBook, book[j].colorBook, book[j].bookColor);

  //this did not work ;( ;( ended up with the books being white.

  var book = [{ /*first book*/
      title: "Vampirates",
      stars: 4,
      bookColor: [93, 240, 98],

      author: "Justin Somper"

    },
    { /*Second book*/
      title: "Harry Potter",
      stars: 5,
      bookColor: [235, 240, 93],
      author: "J.K Rowling"

    }, /*thrid book*/
    {
      title: "Junie B Jones",
      stars: 2,
      bookColor: [255, 0, 0],
      author: "Barbra Park"

    },
    { /*fourth book*/
      title: "Cat in the hat",
      stars: 5,
      bookColor: [0, 0, 255],
      author: "Dr. Seus",


    }
  ];
  // draw shelf
  fill(173, 117, 33);
  rect(0, 120, width, 10);
  // draw lots of books
  for (var j = 0; j < 4; j++) {
    fill(book[j].colorBook, book[j].colorBook, book[j].bookColor);
    rect(j * 102, 19, 90, 100);

    //title
    textSize(12);
    fill(0, 0, 0);
    text(book[j].title, j * 104 + 7, 27, 76, 100);
    //author
    textSize(10);
    text(book[j].author, j * 104 + 10, 43, 70, 98);
  }

  for (var i = 0; i < book[i].stars; i++) {
    image(getImage("cute/Star"), 4 + i * 16, 87, 20, 35);
  }

I expect there to be 4 squares (books) on a line (shelf) and the books to have author, title, correct amount of stars at the bottom of them, and them to be in the right of color which would be green, yellow, red, blue. Thank you so much if u can help me it would greatly affect my learning of Java Script.

Upvotes: 0

Views: 42

Answers (1)

Dupocas
Dupocas

Reputation: 21317

You are trying to access colorBook instead of bookColor:

fill(book[j].colorBook, book[j].colorBook, book[j].bookColor);

Upvotes: 1

Related Questions