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

Array and Object : Exercise about Students & grades report

At my place, Workspace doesn't launch very often or doesn't display anything (even when I type a copy-paste version of the teacher)

So I tried to write this code exercise on CodePen.io : (https://codepen.io/Bloodysheep/pen/MQjKeO)

But the reporting of grades never appear, or the print message because of getElementById maybe ?? Or "output" div not recognized? What should I do to make it appear ? A link in the HTML part ?

var message=0; var student=0; var students=[

{ name:"Ségo", track:"Front End Dev", achievement:"37 hours left", grade:"99%" }, { name:"Suli", track:"Back End Dev", achievement:"3800 pts", grade:"100%" }, { name:"Kiko", track:"Front End Dev", achievement:"1100 pts", grade:"50%" }, { name:"Thomas", track:"Front End Dev", achievement:"2200 pts", grade:"60%" }, { name:"Marie", track:"Back End Dev", achievement:"2500 pts", grade:"100%" }, ]

function print(message){ var outputDiv = getElementById('output'); outputDiv.innerHTML= message; }

for (var i=0; i<students.length; i+=1){ student= students[i]; message += "<h2> Student : " + student.name + "<h2>"; message += "<p> Track : " + student.track + "</p>"; message += "<p> Achievement : " + student.achievement + "</p>"; message += "<p> Grade : " + student.grade + "</p>"; }

print(message);

2 Answers

Steven Parker
Steven Parker
243,318 Points

There are a few issues:

  • an extra comma on line 34
  • on line 38, "getElementById" instead of "document.getElementById"
  • the "prompt" on line 51 should be done inside the loop
  • line 42 has an assignment ("=") instead of a comparison ("==")
  • line 59 also has an assignment ("=") instead of a comparison ("==")
  • on line 67 the "break" ends the loop before all students have been checked for a match
  • there's a missing closing brace at the end of the program

Since you're using codepen, have you tried the "Analyze JS" feature? It highlights some of these.

thank you so much for your time and answers !! I still have a lot to practice on -_-"

(even though I still have this weird message :'Invalid left-hand side in assignment' line 54... what does it mean?)

Steven Parker
Steven Parker
243,318 Points

This pen doesn't seem to exhibit that issue, but typically that message occurs if you try to assign something to a formula or function call:

quantity * price = tax;       // invalid left-hand side in assignment
calc(price, tax) = quantity;  // invalid left-hand side in assignment