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 trialSaqib Ishfaq
13,912 Pointswhat's wrong with this? why doesn't it work without creating the new function?
var student;
var report;
var search;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
while(true){
search = prompt('type a student name to search the record, and to end type \'quit');
if (search === null || search.toLowerCase() === 'quit'){
break;
}
}
for (var i=0; i< students.length ; i+=1){
student = students[i];
if (search.toLowerCase() === student.name){
report += '<h2>' +student.name+ '</h2>';
report += '<p>track: '+ student.track+ '</p>';
report += '<p>achievements: ' +student.achievements+ '</p>';
report += '<p> points: ' +student.points+ '</p>';
}
print(report);
}
1 Answer
Steven Parker
231,269 PointsI'm not sure why you would want to avoid creating the function, but it looks like the main issue might be that the loop that does the searching is outside of the loop that asks for a student name.
It needs to be moved inside the other loop so when something other than "quit" is typed, it will look for it and display it.
Saqib Ishfaq
13,912 PointsSaqib Ishfaq
13,912 Pointsi created the function in the end, but just wanted to see wether it works without one! thanks yeh it worked
Steven Parker
231,269 PointsSteven Parker
231,269 PointsIf you did not want to have a "print" function you could substitute the call with code that replicates what it does: