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
Lido Fernandez
10,556 PointsI also added an option named 'list' so the person who is looking for a student can see all the names available.
// my solution is a little different because I used numbers (ID) instead of names.
var dataStudent;
var finalReport = '';
var finalReportID = '';
var search;
var response;
var alertData;
var messageID = '';
function print(message) {
var div = document.getElementById('output');
div.innerHTML = message;
}
function getStudentData (datastudent) {
var message = "<h2> ID: " + dataStudent.ID + "</h2>";
message += "<p> Name: " + dataStudent.Name + "</p>";
message += "<p> Proffession: " + dataStudent.Proffesion + "</p>";
message += "<p> City: " + dataStudent.City + "</p>";
message += "<p> Country: " + dataStudent.Country + "</p>";
return message;
}
function getStudentDataID (datastudent) {
messageID += "<h2> ID: " + dataStudent.ID + "</h2>";
return messageID;
}
while (true) {
search = prompt("Type the ID of the student or type 'quit' for exit. Type 'List' if you want to see all the ID numbers");
response = parseInt(search);
alertData = true;
if (search.toLowerCase() === 'quit' || search === null) {
break;
}
}
for ( var i = 0; i < students.length; i += 1 ) {
dataStudent = students[i];
if (search.toLowerCase() === 'list') {
finalReportID = getStudentDataID(dataStudent);
print(finalReportID);
}
}
for ( var i = 0; i < students.length; i += 1 ) {
idFound = students[i][0];
dataStudent = students[i];
if (dataStudent.ID === response) {
alertData = false;
finalReport = getStudentData(dataStudent);
print(finalReport);
}
}
if (alertData && search.toLowerCase() !== 'list' ) {
alert('Ups! This ID does not exist. Please, check the ID list and try again.');
}
}
1 Answer
Lido Fernandez
10,556 PointsThanks Nejc!
Nejc Vukovic
Full Stack JavaScript Techdegree Graduate 51,574 PointsNejc Vukovic
Full Stack JavaScript Techdegree Graduate 51,574 PointsEdited the original post for code syntax formatting.