Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

whats wrong with this for loop

keep getting an unexpected token +=

if(quiz.index == quiz.movies.length){

              for (i = 0; i < movies.length; i++){

            var x += '<p class="'+quiz.movies[i].right+'">'+quiz.movies[i].name+ ' real rating '+ quiz.movies[i].rating+'</p><br> ';
              }
                document.write(x)

1 Answer

You are declaring a variable inside the loop so it will be declared few times.

if (quiz.index === quiz.movies.length) { // put '===' to be more specific - good practice

    var x; // declare it first, since you are appending a value to it it may contain 'undefined' when you declare it

    for (i = 0; i < movies.length; i++){


            x += '<p class="' + quiz.movies[i].right + '">' + quiz.movies[i].name + 
            ' real rating ' + quiz.movies[i].rating + '</p><br>';

    }


    document.write(x)

}