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

Henri Okajima
Henri Okajima
6,995 Points

My solution to "how list all students with the same name" and print "student not found" message (in portuguese).

// *** estudantes.js:

var estudantes = [ { name: 'João', track: 'JavaScript', achievements: 'ABC', points: 123 }, { name: 'José', track: 'HTML', achievements: 'A+', points: 456 }, { name: 'José', track: 'PHP', achievements: 'A+', points: 789 }, { name: 'Josué', track: 'Java', achievements: '0-', points: 5 }, { name: 'Mathias', track: 'MongoDB', achievements: '0---', points: 2 } ]; var mensagemHTML = ''; var nomeProcurado = '';

function printDOM(mensagem) { var divID = document.getElementById('saida'); divID.innerHTML = mensagem; }

function prepararMSG(indice) { mensagemHTML += '<ol>'; for (var key in estudantes[indice]) { mensagemHTML += '<li>' + key + ': ' + estudantes[indice][key] + '</li>'; } mensagemHTML += '</ol>'; }

while (true) { nomeProcurado = prompt('Digite o nome do aluno ou \'sair\' para sair:'); if (nomeProcurado.toLowerCase() !== "sair") { mensagemHTML = ''; for (var i=0; i<estudantes.length; i++){ if (nomeProcurado === estudantes[i].name) { prepararMSG(i); } } if (mensagemHTML === '') { mensagemHTML = 'O aluno ' + nomeProcurado + ' não foi encontrado.' } printDOM(mensagemHTML); } else { break; } }

// *** index.html:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Projeto 2</title> <link rel="stylesheet" href="css/styles.css"> </head> <body> <h1>Lista de Alunos:</h1> <div id="saida">

</div> <script src="js/estudantes.js"></script> </body> </html>