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 trialIvan Kusakovic
12,197 PointsStudents for loops?
This is the closest I can understand, but this doesn't work.
var message= "";
var student;
var student2;
var students= [
{
name: "Ivan",
track: "Front-End Development",
achievements: "13 badges",
points: 5163
},
{
name: "Sara",
track: "Back-End Development",
achievements: "12 badges",
points: 500
},
{
name: "Mark",
track: "Design",
achievements: "11 badges",
points: 300
},
{
name: "John",
track: "Business",
achievements: "11 badges",
points: 2001
},
{
name: "Rob",
track: "iOS",
achievements: "10 badges",
points: 100
},
{
name: "Rob",
track: "Markic",
achievements: "113123 badges",
points: 100133
}
];
function print(message){
var outputDiv = document.getElementById("output");
outputDiv.innerHTML = message;
}
function getStudentReport(student){
var report = "<h2>Student: " + student.name + "</h2>";
report += "<h2>Track: " + student.track + "</h2>";
report += "<h2>Points: " + student.points + "</h2>";
report += "<h2>Achievements: " + student.achievements + "</h2>";
return report;
}
while(true){
var pr1 = prompt("Enter name or quit to close:");
if (pr1 ===null || pr1 === "quit"){
break;
}
for (var i = 0; i<students.length; i+=1){
for (var j=0; j<students.length; j+=1){
if (students[i].name === students[j].name ){
message= getStudentReport(students[i]) + getStudentReport(students[j]);
print(message);
}else if (students[i].name ===pr1) {
student= students[0];
message=getStudentReport(student);
print(message);
}
}
}
}
Can anyone explain me am I on good way?
Cheers
3 Answers
Jesus Mendoza
23,289 PointsHey Ivan, I came with this solution. It is basically the same code as the video except for the print, i move it outside the if, so every time it loops through the for loop it will print whatever it is in the message variable.
var message = '';
var student;
var studentName;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function getStudentReport(student) {
var report = '<h2>Student: ' + student.name + '</h2>';
report += '<p>Track: ' + student.track + '</p>';
report += '<p>Points: ' + student.points + '</p>';
report += '<p>Achievements: ' + student.achievements + '</p>';
return report;
}
while(true) {
studentName = prompt("Whats the name of the student?");
if (studentName.toLowerCase() === 'quit' || studentName === null) {
break;
}
for (var i = 0; i < students.length; i += 1) {
student = students[i];
if (studentName.toLowerCase() === student.name.toLowerCase()) {
message += getStudentReport(student);
}
print(message);
}
}
Ivan Kusakovic
12,197 PointsFantastic! I thought that is something like that. Thanks :)
Ivan Kusakovic
12,197 PointsHey, but I wanted to check if there is two identical names :) Jesus :)