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 trialYixi Guan
7,927 PointsMy solution for the final challenge - how to improve?
The code is short, and it seems to work. It tells how many results are found, and shows the results if there are any. I ended up with using no functions (I just couldn't get it work :( ). I wonder if anybody can help improving it.
var students = [
{
name: 'brooklyn',
track: 'Front-end Web Developer',
achievements: '22',
points: '2900'
},
{
name: 'dustin',
track: 'PHP Developer',
achievements: '10',
points: '1000'
},
{
name: 'wallace',
track: 'Android Developer',
achievements: '1',
points: '99'
},
{
name: 'dustin',
track: 'Web Designer',
achievements: '5',
points: '500'
},
{
name: 'brooklyn',
track: 'Full stack JavaScript developer',
achievements: '50',
points: '5000'
},
{
name: 'brooklyn',
track: 'Andiod developer',
achievements: '50',
points: '5000'
}
];
var message;
var student;
var search;
var counter;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
while (true) {
message = ''; //set message to empty every time run the while loop.
counter = 0; //set counter to 0 every time run the while loop.
search = prompt('input a student name, type quit to end.');
if (search === null || search.toLowerCase() === 'quit'){
break;
}
for (var i = 0; i < students.length; i++ ) {
student = students[i];
if ( search.toLowerCase() === student.name.toLowerCase() ) {
counter += 1;
message += '<h3>Student: ' + student.name + '</h3>' ;
message += '<p>Track: ' + student.track + '</p>' ;
message += '<p>Achievements: ' + student.achievements + '</p>' ;
message += '<p>Points: ' + student.points + '</p>';
}
}
message = 'Found ' + counter + ' result(s).' + message;
print(message);
}
3 Answers
Michael Braga
Full Stack JavaScript Techdegree Graduate 24,174 PointsLooks good to me, maybe try adding that message function or try the extra challenges now.
Ray Wai
2,355 PointsThis is a really good answer
Yixi Guan
7,927 PointsThank you. :)
Saghir Hussain
8,765 PointsHave you figured out how to show more than one record where students have the same first name? You could do it by including the print message within the for loop rather outside? Thoughts?
Yixi Guan
7,927 PointsYixi Guan
7,927 PointsI tried to use the message function, but it wouldn't work. It only shows only one result when there are more than one person use the same name. ;-(