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

Doegena Fennich
Doegena Fennich
8,974 Points

How should the structure of JavaScript look like.

Hey, i tried to solve the problem of the last challenge but didn't work out as i wanted, so i asked the teacher if he could look what the problem was... The first answer that i got was that the structure was messy and that i should put var String message instead of var message.

So can anyone show me an example how the JavaScript structure should look like.

This is the code i showed him and if you can help me what i did wrong would be great to.

var message = '';
var student;
var search;
var trackStu;

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

function getStudentReport( student ) {
  var report = '<h2>Student: ' + student.name + '</h2>';
  report += '<p>Track: ' + student.track + '</p>';
  report += '<p>Points: ' + student.points + '</p>';
  report += '<p>Achievements: ' + student.achievements + '</p>';
  return report;
}

while (true) {
  search = prompt('Search student records: Type a name [Jody] and type the track the student follows. (or type "quit" to end)');
  trackStu = prompt("Type in the track the student follows.");
  if ( trackStu.toLowerCase() === 'quit' || trackStu === null || search === null ) {
    break;
  } 

  for ( var i = 0; i < students.length; i += 1 ) {
    student = students[i];
    if ( student.name == search && student.track == trackStu ) {
      message = getStudentReport(student);    
      print(message);
      break;

    }    
   }


}
var students = [ 
  { 
    name: 'Dave',
    track: 'Front End Development',
    achievements: 158,
    points: 14730
  },
  {
    name: 'Jody',
    track: 'iOS Development with Swift',
    achievements: '175',
    points: '16375'
  },
  {
    name: 'Jordan',
    track: 'PHP Development',
    achievements: '55',
    points: '2025'
  },
  {
    name: 'Jody',
    track: 'Learn WordPress',
    achievements: '40',
    points: '1950'
  },
  {
    name: 'Trish',
    track: 'Rails Development',
    achievements: '5',
    points: '350'
  }
];

Thanks in advance

1 Answer

Steven Parker
Steven Parker
231,110 Points

You may have gotten some bad advice.

The expression "var String message" doesn't make sense. Who suggested that? Is that in another question you asked here?

But if you wanted to see an example of a completed assignment, the instructor's version can be seen in this video: The Student Record Search Challenge Solution. The main difference is that the original assignment looks up students by name only and does not use the track to search.

Doegena Fennich
Doegena Fennich
8,974 Points

Hey Steven Parker , yeah i thought so to. Funny part is the teacher thats said "what i made is comepletely wrong" doesn't know know much of JS. while the other teacher(a lot of experience with js) told me that this code structure is good, it only got some minor issues.

Think i panicked when he responded with "this code structure is badly done."

edit: looking more in to it i see the problems the code had.