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

Personal Project Help: If/Else Statements, User Prompts

I have a button that when clicked will prompt the user with a question. If the answer matches the variable name, I would like to display all of the objects of that variable, back to the user. Is this possible?

Please note: I only know how to display 1 object from the variable, but not all of them. Where it says ewers.email.

function employee(){

  var ewers = {
  firstName: "Eric",
  lastName: "Ewers",
  email: "eric.p.ewers@gmail.com"
};

prompt("Search by last name")
if (string equals "ewers") {
    document.getElementById("results").innerHTML = ewers.email;
  } 
  else {
    document.getElementById("results").innerHTML = "Could not find a match";
  }
}

4 Answers

Try changing the line to the following:

document.getElementById("results").innerHTML = ewers.firstName+' '+ewers.lastName+'<br/>'+ewers.email;
var person=prompt("Search by last name");
if (person == "ewers") {
    document.getElementById("results").innerHTML = ewers.firstName+' '+ewers.lastName+'<br/>'+ewers.email;
  } 
  else {
    document.getElementById("results").innerHTML = "Could not find a match";
  }

Let me know if that was what you are looking for?

This works perfectly! Thank you!

Yeah that would work. Sorry I forgot to mention more importantly that I need to match the value that the user gives me to the variable. If it matches then display everything, which you did nicely, thanks. But... If it doesn't match, then run the else statement.

I will check tomorrow (about 8 hours from now) and let you know if that worked. I'm on my phone.

Sounds good!