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 trialGustavo Buarque
7,113 PointsCould you review my solution?
var html = "";
var student;
var stuNames = [];
function print(message){
var div = document.getElementById("output");
div.innerHTML = message;
}
function studentNExist(arr,resp){
var nfound = false;
var onlyNames = [];
for( var i = 0; i < arr.length; i++){
onlyNames.push(arr[i].name.toLowerCase());
}
if(onlyNames.indexOf(resp) < 0){
nfound = true
}
return nfound;
}
while(true){
var response = prompt("Search student records: type a name 'Jody' (or type 'quit' to end)").toLowerCase();
if(response === 'quit' || response === null) {
break;
} else {
for(var i = 0; i < students.length; i++) {
student = students[i];
if(student.name.toLowerCase() === response){
html += "<h3>Student: " + student.name + "</h3>";
html += "<p>Track: " + student.track + "</p>";
html += "<p>Points: " + student.points + "</p>";
html += "<p>Achivements: " + student.achivements + "</p>";
print(html);
}
}
if(studentNExist(students,response)){
print("There is no student called " + response );
}
}
html = "";
}
4 Answers
Thomas Nilsen
14,957 PointsGood code :)
My main suggestion is you can make the search function a lot smaller:
from this:
function studentNExist(arr,resp){
var nfound = false;
var onlyNames = [];
for( var i = 0; i < arr.length; i++){
onlyNames.push(arr[i].name.toLowerCase());
}
if(onlyNames.indexOf(resp) < 0){
nfound = true
}
return nfound;
}
to this (i changed the method name as well):
function isFound(arr,resp){
return arr.join(' ').indexOf(resp) > -1 ? true : false;
}
and call it like this:
if(!isFound(students,response)){
print("There is no student called " + response );
}
Thomas Nilsen
14,957 Pointshm - are you sure?
here is an example:
var students = ['thomas', 'joe', 'john'];
function isFound(arr, search) {
return students.join(' ').indexOf(search) > -1 ? true : false;
}
console.log(isFound(students, 'thomas')); //prints true
Gustavo Buarque
7,113 PointsI'll see and I'll give you a feedback soon.
Gustavo Buarque
7,113 PointsThe array have objects inside, because this does not work.
Thomas Nilsen
14,957 PointsMy mistake - I didn't realise it was an array of objects.
My function still works, with one tiny change though. We need to add a map-method first;
var students = [{name: 'thomas'}, {name: 'joe'}, {name: 'john'}];
function isFound(arr, search) {
return students.map(obj => obj.name).join(' ').indexOf(search) > -1 ? true : false;
}
console.log(isFound(students, 'thomas')); //prints true
Gustavo Buarque
7,113 PointsGustavo Buarque
7,113 PointsInteresting but when I execute the message "There is no student called" is applied to all responses.
When I created 2 "fors" it was because did not work using the main "for". I have tried everything but I could not resolve that.