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

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

Guiomar Almunia
PLUS
Guiomar Almunia
Courses Plus Student 7,062 Points

The Student Records Search extension. Help!!

Can anyone help me with this? When I add an else statement to produce an alert if the student is not in the database, I always get the alert of the else statement first, even when the condition is true and then, I get the properties of the student I typed. So first the else statement and then the if statement. Very weird. By the way, I have no idea to construct this programm when we have two students with the same name. ¿Any suggestions?

Here is my code.

var students =[ {name: 'Lucy', track: 'IOS', achievements: 2, points: 100}, {name: 'Anna', track: 'Web Developer', achievements: 5, points: 50}, {name: 'Cora', track: 'Wordpress', achievements: 3, points: 75}, {name: 'Alex', track: 'JavaScript', achievements: 1, points: 99}, {name: 'Matthew', track: 'HTML', achievements: 4, points: 133}

];

var student; var message=' '; /* STRING DE HTML QUE CONSTRUIREMOS CON EL LOOP / var busqueda; / VARIABLE PARA ALMACENAR LA BÚSQUEDA DE CADA ESTUDIANTE */

function print(message){ var outputDiv= document.getElementById('output'); outputDiv.innerHTML = message; }

function studentInforme(student){ /* el parámetro es el objeto student */ var informe = "<h2>Student: " + student.name + "</h2>"; informe += "<h2> Track: " + student.track + "</h2>"; informe += "<h2> Achievements: " + student.achievements + "</h2>"; informe += "<h2> Points: " + student.points + "</h2>"; return informe;

} while(true){ busqueda = prompt("Busca a tus estudiantes: Escribe un nombre o escribe 'quit' para terminar el programa"); if(busqueda===null || busqueda.toLowerCase()=== 'quit'){ /* en ambos casos la condicion se cumple y se acaba el bucle */ break; }
for(var i =0; i< students.length; i+=1){ student=students[i]; if(busqueda === student.name){ message = studentInforme(student); print(message); }else{ alert("El nombre del estudiante " + busqueda + " no aparece registrado en la base de datos"); } } } print(message);

Ptda: Some of the coments and some of the variable names are in Spanish because I am from Spain so it´s easier for me to declare some variables in my languaje.

2 Answers

Steven Parker
Steven Parker
231,007 Points

Every time you check to see if you have found the student, you alert the "not found" message but keep checking.

You want to show "not found" only after you have checked for a match with every student. If you do this with a "found" flag, you can also handle students with the same name (you will get them all).

Also, you must append the message, not just assign it, or you will only see the last student searched for.

And you only need one print. Inside the loop if you want to see the page being built, outside if you want to see it only after "done".

Here's the replacement for the for loop:

    var found = false;
    for (var i = 0; i < students.length; i += 1) {
        student = students[i];
        if (busqueda === student.name) {
            message += studentInforme(student);
            print(message);     // if you print here, you don't need the one after the while loop
            found = true;
        }
    }
    if (!found)
        alert("El nombre del estudiante " + busqueda + " no aparece registrado en la base de datos");