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 Solution

code does not work

```var message = ''; var student; var search;

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] (or type quit to end)'); if (search === null || search.toLowerCase() === 'quit'){ break;

}

for (var i = 0; i < students.length; i += 1) { student = students[i]; if (student.name === search ){

message = getStudentReport( student );
print( 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>'; }

It was working for last video but I followed along and added the new lines as per the video and it did not work can someone help out please?

4 Answers

Simon Coates
Simon Coates
28,694 Points

didn't really read through it as the formatting is garbled but

var report  += 

looks like a mistake. you're declaring a variable and using += which assumes the variable already exists. Once there's something in the variable you can use +=, but to begin with your probably want =.

eg. The following is a snippet i ran in the developer console.

var x += "5";
VM1073:1 Uncaught SyntaxError: Unexpected token +=
var x = "5";
undefined
x;
"5"
x+= "6";
"56"

many thanks for your reply Simon. I will change += to =. Sorry it was garbled I think I put code between code

I think maybe it should have been code\ but do not see option to edit the post.

Simon Coates
Simon Coates
28,694 Points

I think treehouse mods fix formatting a lot of the time. I don't know if the 'markdown' is the same across the board, but when answering questions, i think it's three backticks (the key with the tilde) on a line before and after code. If you have more trouble with code, you might try posting a url to a 'workspace snapshot'. It allows people to clone your code and debug. In any case, you're more likely to get help.

Also it's useful to provide information about the glitch to debuggers eg. didn't do what you wanted vs caused an exception/Error on a specific line. It might be the difference between just a simple read through to see a syntax error versus needing to think about how it would run for a more dynamic error.

Also, you should be able to trace program flow (breakpoints etc) and variable values within your browser's developer tools. Or test a function on its own so that you know it's reliable (treating the bits of code as building blocks, working with a process of elimination to isolate the error). A more naive approach might be to store variable in the global scope (so you can inspect them after execution) and use console.log statement to trace execution. It may be painful, but debugging skills are super important going forward, and something i think treehouse doesn't stress enough.

Hi Simon, thank you for your reply; I agree debugging is very important . I can debug easily in the IDE of ms excel for vba as there are plenty of youtube videos on this topic. Unfortunately I still have no idea how to debug javascript in the browser console due to lack of quality instruction.

Simon Coates
Simon Coates
28,694 Points

as discussed the naive approach is to use console.log (or the alert command) to indicate variable values and which branch of your code executes. THe output should be visible in the console tab of your developer tools. (see https://www.w3schools.com/js/js_debugging.asp ) . The console may also display errors or exceptions and the relevant line numbers. You can also write javascript code (as lines or blocks) directly into the console. It's useful for quick experiments and debugging.

To do proper debugging in chrome developer tools, you can open developer tools, navigate to the source tab to locate the file containing javascript. Clicking in the margin of your custom javascript lets you set a break point, so the code stops on that line (if the line is reached during execution). You can then use the step commands *(over, in, out, run) to execute your code line by line, and observe the state of variables during an execution (hover on variable name). Google has a text tutorial that does line or event based breakpoints at https://developers.google.com/web/tools/chrome-devtools/javascript/ .

  • these should be visible and most commonly have an arrows in the icons.

Hi Simon Thanks for your reply. The problem is I can't locate the js file I try selecting from the source as you mention but it doesn't appear there so until I can do that I can't proceed. .

Simon Coates
Simon Coates
28,694 Points

When you open a html page, the files that it's built from should appear in the source tab. There's an area to select them labelled 'source'. If you want me to actually do the debug, it would be helpful if you could give me a workspace snapshot url (see https://teamtreehouse.com/library/previews-and-snapshots ). The reason why i didn't debug for you was because i was clearly looking at partial code (and it was 11.15pm here). Its correct operation requires the .html file (to have a specific DOM structure, and to include the js files it uses) and - i think - another .js file that defines the student list. These also need to be wired up correctly (order may matter) before you get to the issue of whether the code is defective.