-4

So I have this project for my class and all I have to do is display one movie and review and the second one under the first one. I think my formatting is incorrect and also my size values. Thanks in advance!

Now, to get both your movies displaying, use a for loop to iterate through the array and execute the text commands for each movie. If you don't remember how to iterate through arrays, re-watch the previous talk-through and/or Looping through Arrays. HintWhat's this?

 for (var  =; ;  ) {
     fill(84, 140, 209);
     textAlign(CENTER, CENTER);
     textSize(20);
     text(, 200, *+);
     textSize ();
     text(, 200, *+);

My code...

 var movies = [
     {
         title: "Puff the Magic Dragon",
         review: "Best movie ever!!"
     },
     {
         title: "Batman",
         review: "Second best movie!!"
     }
 ];

 fill(84, 140, 209);
 textAlign(CENTER, CENTER);
 textSize(20);
 text(movies[0].title, 200, 50);

 fill(84, 140, 209);
 textAlign(CENTER, CENTER);
 textSize(20);
 text(movies[0].review, 200, 100);

 for (var movies = 100; 100; 100){
     fill(84,140,209);
     textAlign(CENTER, CENTER);
     textSize(20);

     text(movies[0].title, 200,100*100+50);

     textSize (10);
     text(movies[0].review, 200,100*100+100);
 }
  • 1
    What's your question? – rageandqq Apr 14 '18 at 22:40
  • Only the Puff The Magic Dragon "Best movie ever!!" is displaying and I'm supposed to have "Batman" "Second best movie" displaying under it as well. – Jessica Lang Apr 14 '18 at 22:47
  • @JessicaLang Welcome to StackOverflow. Make sure to upvote if my answer has helped you. I'd highly recommend going back and reviewing the lecture, rather than just copying and pasting; otherwise, you're in for a rough semester! – Trent Apr 14 '18 at 23:10
  • @JessicaLang in the code it says `movies[0]` twice but it is meant to say `movies[0]` once and `movies[1]` ones as well – Rinzler786 Jan 02 '19 at 21:23

3 Answers3

0

In the code it says:

text(movies[0].title, 200,100*100+50);

textSize (10);
text(movies[0].review, 200,100*100+100);

On the bottom text function you need change the movies[0] to movies[1].

Rinzler786
  • 109
  • 1
  • 11
0

try these:

for (var i = 0; i < movies.length; i++){
fill(84, 140, 209);
textAlign(CENTER, CENTER);
textSize(20);
text(movies[i].title, 200, i*100 + 50 );
textSize(15);
text(movies[i].review, 200, i*100 + 100);

}

  • 2
    Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. – DCCoder Nov 02 '20 at 17:47
-1

Only the first movie element is displaying from your object array, because you're only ever indexing the 0th element!

If you have an array as follows,

var movies = [
     // referred to by movies[0]
     {
         title: "My first movie",
     },
     // referred to by movies[1]
     {
         title: "My second movie",
     }
 ];

notice that each element in the array is referred to with its appropriate index. (i.e. movies[0], movies[1])

Do you see the issue now?


Hint

Your array is invalid!

The statement for (var movies = 100; 100; 100) { } doesn't have a conditional or a stopping point.

Instead, it should be structured as follows:

for (var myCounter = startingVal; myCounter < myStoppingConditional; myCounter++) { 
    // run whatever is in here as many times as it takes
    // to get from myCounter to myStoppingConditional
};
Trent
  • 4,208
  • 5
  • 24
  • 46
  • Thank you for the comment! Am I on right right track? for (var i = 0; < positions.movies; i++){ fill(84,140,209); textAlign(CENTER, CENTER); textSize(20); text(movies[0].title, 200,100*100+50); textSize (10); text(movies[0].review, 200,100*100+100); } – Jessica Lang Apr 14 '18 at 23:11
  • Almost! Two issues. There is no such thing as `positions`; nor a property of `movies` on that nonexistent variable. Lookup "javascript get amount of objects in an array" and use that instead of `positions.movies` in the `for` statement. Then, notice that you're still printing out `movies[0]`. Try swapping that `0` for a variable instead of a static number. HINT: you already have the correct variable that you should use! – Trent Apr 14 '18 at 23:34
  • I tried changing [0] to [10] and positions.movies to title.movies but it's obviously not working, i'm about to rip my hair out ugh – Jessica Lang Apr 14 '18 at 23:43
  • Why [10]? Do you have 11 elements in your array? Instead, you want to swap 10 for a variable which represents the index of each of the elements you're looping through, dynamically as you loop through the items. That way, when you're looping through the 0th element (`movies[0]`), you're printing out the corresponding object properties, and then the second element (`movies[1]`), etcetera... – Trent Apr 15 '18 at 02:15
  • Did you try looking up what I suggested? You need to swap `position.movies`, which isn't a thing, for a stopping condition that is the number of movies you have in your `movies` array. – Trent Apr 15 '18 at 02:15