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 JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge

Aaron Glaesemann
Aaron Glaesemann
11,303 Points

I was having trouble running my code in a browser. So I tried the instructor's code instead and theirs also won't run.

I've made sure that AdBlock is disabled on both of the browsers I used (Chrome and Safari) plus I also checked my settings to make sure that JavaScript is enabled (which it is). However, the code isn't behaving the same way that Dave's code was in his videos. All I'm getting is <h1> tag of "Students" to appear. The JavaScript itself isn't executing it seems.

Thomas Nilsen
Thomas Nilsen
14,957 Points

Can you show us the code you're trying to run?

Aaron Glaesemann
Aaron Glaesemann
11,303 Points

It was the code downloaded from the challenge Workspace.

Could you get the Workspace code to work?

This is the HTML file, index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Students</title>
  <link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>Students</h1>
<div id="output">

</div>
<script src="js/students.js"></script>
<script src="js/student_report.js"></script>
</body>
</html>

This is the JavaScript file named, student_report.js. It processes the students.js script.

var message = '';
var student;

function print(message) {
  var outputDiv = document.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>Points: ' + student.points + '</p>';
  message += '<p>Achievements: ' + student.achievements + '</p>';
}

/* Print function was not included in Workspace code. */
print(message);

Here's the students.js file which is the data collection used by student_reports.js.

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: 'John',
    track: 'Learn WordPress',
    achievements: '40',
    points: '1950'
  },
  {
    name: 'Trish',
    track: 'Rails Development',
    achievements: '5',
    points: '350'
  }
];

Essentially, I can't get this code to run through the Workspaces in both Safari nor Chrome. Furthermore, I copied the code to WebStorm to see if it would give me any errors. It let me know that the print() function wasn't being utilized. So I called it at the end of the student_report.js file just to be sure. Still no luck.

1 Answer

Steven Parker
Steven Parker
229,732 Points

Adding the call to print was a good idea. With that included, the code works fine for me.

So I had to wonder if your JavaScript files are not being included. Are they actually located in a folder named "js", and that folder shares the location of index.html?

Aaron Glaesemann
Aaron Glaesemann
11,303 Points

Thanks for running that for me. That at least lets me know that it must be something on my side. Yeah, I have all of my directories correct. I'll try another browser like Firefox or Opera to try and determine what my issue may be. Again, I appreciate the help.

Edit: Also, strangely enough... I can access the property values through the console in both Safari and Chrome. So I know the JavaScript files are linked. I just don't understand why it isn't executing for me. Why did I ever leave Xcode? ... :D

Edit 2: Figured it out, sort of. I'm not sure why it's behaving this way but essentially once I load the page it won't work but I have to refresh the browser to execute the JavaScript. It wasn't behaving this way before but now it is. It used to just execute the JavaScript the first time. Anyway, I'm glad that I've now found some resolution. Thanks again for the help.