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 trialStefan Cutajar
7,747 PointsCreating a two dimensional array using .push method (or best way to do it).
Hi guys, I'm currently working on the last challenge of objects and I'm trying to add an array in an array so then I can loop through it to print the data however I can't find out how to do this. Im aware that I have some commented out code and unused variables because I've been experimenting for a while on this challenge trying to figure it out. Here you can find my work space ->> http://w.trhou.se/nwr3k26vgd Thanks, Stefan
4 Answers
Steven Parker
231,268 PointsIn case you were wondering what the issue was in the original code, on line 20 of "student_report.js" the same loop variable "i" that is used to index the "students" array is being used to index into the "response" array. This means that a student can only be found if the search terms are entered in the same order as the students are stored ("Dave" first, "Jody" second, etc).
brendon hanson
6,191 PointsI did this a few months ago and had issues too. Here's how I would do it. I didn't finish everything so you could get some practice with it but I hope this helps! Make sure to mark best answer if you think I helped.
var student;
question = prompt('Please type the student name you wish to search for or type quit to exit');
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function getStudent(student) {
let report = `<h1>NAME: ${student.name}</h1>`;
`<ol class="list">`
report += `<li>Membership: ${student.track}</li>`;
`</ol>`
return report;
}
for ( i = 0; i < students.length; i ++){
student = students[i];
if (question === student.name){
message = getStudent(students[i]);
print(message);
} else if( question === "quit" ){
}
}
If you want me to explain why this works just ask :)
Stefan Cutajar
7,747 PointsThanks for your reply however I would prefer if I could get my code to work so I can learn from it.
brendon hanson
6,191 PointsHere is the finished code. I don't know if this was your desired functionality but I am pretty sure this is. If the prompt doesn't show up just refresh the page(I'm sure you already knew that) Just replace your old code with this and it should be good!
var student;
question = prompt('Please type the student name you wish to search for or type quit to exit');
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function getStudent(student) {
let report = `<h1>NAME: ${student.name}</h1>`;
`<ol class="list">`
report += `<li>Track: ${student.track}</li>`;
report += `<li>Achievments: ${student.achievements}</li>`;
report += `<li>Points: ${student.points}</li>`;
`</ol>`
return report;
}
for ( i = 0; i < students.length; i ++){
student = students[i];
if (question === student.name){
message = getStudent(students[i]);
print(message);
} else if( question === "quit" ){
}
}
Again if you want me to explain how it works just ask. There are other ways to do this as well but this is the easiest way to me
Stefan Cutajar
7,747 PointsThanks you for your help guys :)
brendon hanson
6,191 Pointsbrendon hanson
6,191 PointsIt should work fine. All you have to do is add a few more "report +=
<li>blah blah</li>
". I can finish the rest it would only take 5min.