Profile lookup checking

Both conditions are correct but one overrides the other, how can i make it so when the fist condition is false it checks for the other condition. i tried else if but it overrides the first condition, maybe instead of for loop it should be a different loop idk

  **Your code so far**

// Setup
var contacts = [
  {
      "firstName": "Akira",
      "lastName": "Laine",
      "number": "0543236543",
      "likes": ["Pizza", "Coding", "Brownie Points"]
  },
  {
      "firstName": "Harry",
      "lastName": "Potter",
      "number": "0994372684",
      "likes": ["Hogwarts", "Magic", "Hagrid"]
  },
  {
      "firstName": "Sherlock",
      "lastName": "Holmes",
      "number": "0487345643",
      "likes": ["Intriguing Cases", "Violin"]
  },
  {
      "firstName": "Kristian",
      "lastName": "Vos",
      "number": "unknown",
      "likes": ["JavaScript", "Gaming", "Foxes"]
  }
];


function lookUpProfile(name, prop){
// Only change code below this line

for(var i=0; i < contacts.length; i++){
  if(name == contacts[i]['firstName']){
    if(contacts[i][prop]){
        return contacts[i][prop];
     
 }
}else if(name !== contacts[i]['firstName']){
    return "No such contact";

}
}
// Only change code above this line
}

lookUpProfile("Akira", "likes");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

I think the problem is in the second if statement where you use the !== operator instead of !=. That must be why your second condition is not running

1 Like

No, it is preferable to use !==. (Also preferable to use ===)

The issue is that your code will stop the very first time it encounters a return, so you will only check one contact.

1 Like

You are right. It is better to assign the response to a variable and return the result outside the loop.

2 Likes

Yeah, that would work. Or moving one return statement. Might be other ways too. Whichever feels more natural to @jaime.tavekz

2 Likes

i feel like if i assign the response to a variable that would make the code more complicated then it has to be.

i’ll try your way :slightly_smiling_face:

It might. But one way or another you need to only return ‘No Such Contact’ only happens after you check every contact.

2 Likes

== vs === have specific uses separate from each other, it isn’t fair to simply say it’s preferable without an explanation

2 Likes

yeah i noticed most times i’m not sure which one to use as they both seems similar. is === use when it is strict and u don’t expect the data to change types?

To be exact, you should generally use ===.

The === operator is a strict comparison. It checks that your two values are the same value and type.

The == operator is a weak comparison. It checks that your two values have the value, but it may do a type conversion for you.

== may produce unintended results if you aren’t very careful, so I always recommend ===.

2 Likes

There is a difference. The === compares the value and type of variables, while == only compared the value. checkout this link

https://www.w3schools.com/js/js_comparisons.asp

1 Like

Also when accessing the objects I use bracket notation because i’m thinking the input is changed then it can be flexible. Am i correct i’m not sure if using the bracket notation or the dot notation on my conditions , which one is the right one.

You’re right, in this case you’ll need bracket notation. Dot notation only works when you have the exact property name when witing the code.

1 Like

considering you know your object keys. both of them work just fine. It depends on your preference

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.