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
Damian Szymanski
11,843 PointsPart of Javascript code not running.
Hello!
I've been trying to finish this project for a few days and I keep getting stuck. I fixed up my code from the last forum post but now I only get the "else if" message in the searchInfo() function.
really could use some help!
Codepen: https://codepen.io/Melon_Max/pen/xejZoy
Thank you! Damian.
5 Answers
Antony .
2,824 PointsI apologize for the code I gave you above. I was tired and it was late. I've refactored your code again, but this time it should work the way you want it to. Also if the code or comments doesn't make any sense to you, please don't hesitate to ask. I'm always here to help.
var user;
var message = "";
function print(message) {
var div = document.getElementById("output");
div.innerHTML = message;
}
function getSearchPrint(user, student) { //Add a second parameter. The second parameter references what you inserted inside your input box.
var report = '';
for (var i = 0; i < user.length; i += 1) {
if(student === user[i].name) { //here add a conditional statement to test if the "student" equals any of the names in your for loop.
report += "<h2> name: " + user[i].name + " </h2>";
report += "<p> Join Date: " + user[i].joinYear + " </p>";
report += "<p> Number of Videos: " + user[i].numVid + " </p>";
}
}
return report;
}
function searchInfo() {
var searchInput = document.getElementById('search').value;
var found = false;
for (var i = 0; i < users.length; i++) {
user = users[i];
if (searchInput === user.name) {
found = true;
message = getSearchPrint(users, user.name); //replace "+=" with "=" or else you'll keep on adding up users when you search, and in your function add a parameter called user.name
print(message);
} else if(!found) {
print("<h2>No user named '" + searchInput + "' was found. Please try again.</h2>");
}
}
}
var users = [
{
name: "damian",
joinYear: 2019,
numVid: 39
},
{
name: "monika",
joinYear: 2020,
numVid: 44
},
{
name: "leaf",
joinYear: 2018,
numVid: 34
},
{
name: "looksy",
joinYear: 2018,
numVid: 12
}
];
Damian Szymanski
11,843 PointsThank you so much! You helped ALOT.
Hopefully see you are around in the community! Damian.
Antony .
2,824 PointsAntony .
2,824 PointsI left a few comments hinting what you did wrong.
Happy coding!
var user;
var message = "";
function print(message) {
var div = document.getElementById("output");
div.innerHTML = message;
}
function getSearchPrint(user) {
var report = '';
for (var i = 0; i < user.length; i += 1) {
if (user1.name === user[i].name) { // I'm not sure what you meant to do in this conditional statement but this would not return true, hence your code underneath isn't running. I'd suggest removing your conditional statement.
//user.name, user.joinYear, user.numVid will return undefined because you forgot to include [i]. Remember, [i] iterates over your array of objects.
report += "<h2> name: " + user.name + " </h2>";
report += "<p> Join Date: " + user.joinYear + " </p>";
report += "<p> Number of Videos: " + user.numVid + " </p>";
}
}
return report;
}
function searchInfo() {
var searchInput = document.getElementById('search').value
for (var i = 0; i < users.length; i += 1) {
user = users[i];
if (searchInput === user.name ) {
message += getSearchPrint(user); //In your getSearchPrint function parameters you're supposed to reference your "users" variable that has a list of students. You just forgot a 's'.
print(message);
} else if (searchInput !== user.name) {
print("<h2>No user named '" + searchInput + "' was found. Please try again.</h2>");
}
}
}
var users = [
{
name: "damian",
joinYear: 2019,
numVid: 39
},
{
name: "Monika",
joinYear: 2020,
numVid: 44
},
{
name: "Leaf",
joinYear: 2018,
numVid: 34
},
{
name: "Looksy",
joinYear: 2018,
numVid: 12
}
];
Damian Szymanski
11,843 PointsHello! sorry to keep bothering you, but I fixed all the comments that you left and I still am getting the "else if" message in the searchInfo() function :(. I pasted my code in a new pen and I could really use your help again!
CodePen: https://codepen.io/Melon_Max/pen/MRGQVX
Sorry to bug you, Damian.
Damian Szymanski
11,843 PointsNo thank you! I just have one question about the code. What does the second parameter in the getSearchPrint(); function do? Im having trouble understanding the comment.
Thank you so much! Damian.
Antony .
2,824 PointsYup, so let's boil this down.
Take a look at this function:
function getSearchPrint(user, student) {
}
So we know that the first parameter (user) is referencing the variable users aka your array of objects, because you called the getSearchPrint function with a value of users, inside your searchInfo function.
message = getSearchPrint(users, user.name);
^^^^
Now for the second parameter (student) you can see that we called the getSearchPrint function with a second value of user.name, your user.name derives from your for loop inside your searchInfo function
message = getSearchPrint(users, user.name);
^^^^^^^^
So I essentially grabbed the user variable and got access to the name property and added it to the getSearchPrint function
for (var i = 0; i < users.length; i++) {
user = users[i]; //this line
}