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 trialKevin Faust
15,353 PointsI believe my code is correct but nothing is being displayed. I dont want to view the solution just yet. any help please
This is just the students array of objects. same as dave's. nothing wrong here but just put here in case needed for reference to my main code
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'
}
];
Here is my main code and Ive looked over it several times and did several edits and everything seems logical but I must be overlooking something. any help is always appreciated. cheers
var message = '';
var student;
var response;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
while (true) {
response=prompt("Search student records: type a name or type 'quit' to end");
if (response.toUpperCase() === "QUIT") {
break;
}
for (var i = 0; i < students.length; i += 1) {
student = students[i];
if (student.name.toUpperCase() === response.toUpperCase()) {
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(message);
}
}
Kevin Faust
15,353 PointsHey Justin,
This is my workplace snapshot: https://w.trhou.se/t2sfvp8cec Since my time of posting, i managed to get the major errors out of the way. For the most part it's working except for 1 (minor) thing. When I type a name into the prompt, it successfully shows the information on the screen. However when I type another name, instead of replacing what's already on the screen, the information gets placed below. And so if I type 10 names into the prompt, it creates a really long page of information which isn't supposed to happen.
If you have a solution and/or other tips/feedback, it would be greatly appreciated!! Cheers
1 Answer
Justin Iezzi
18,199 PointsI checked it out, looks good! Only two things worth mentioning I think. In your while condition, "true" is a perfectly fine argument in this case, you don't have to create a boolean variable for it. And the formatting needs some work! :P
var student;
var response;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
while (true) {
response = prompt("Search student records: type a name or type 'quit' to end");
if (response.toUpperCase() === "QUIT") {
break;
} else {
for (var i = 0; i < students.length; i += 1) {
student = students[i];
if (student.name.toUpperCase() === response.toUpperCase()) {
var 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(message);
}
}
}
}
As for your issue, it keeps adding them because your message variable keeps storing strings without ever being reset. By placing the variable declaration inside of the if statement, we're basically resetting the variable and message each time. I made the change to fix this in the code above. If you don't like recreating the variable every time (like me), you can turn the first += operator into a =. This will also erase the entire previous string.
Glad you were able to get it working!
Kevin Faust
15,353 PointsThanks a ton :)
Haha yea my formatting went from good at the start to a bit of a mess. During my 2 hours of trying to solve this challenge , I guess I got slowly more frustrated and lazy. And the 2 things you mentioned just flew by my head. Thanks for pointing it all out! Cheers
Justin Iezzi
18,199 PointsJustin Iezzi
18,199 PointsA workplace snapshot would make this a lot easier to test. Post it back here and I'll check it out for you!