[SOLVED] Profile Lookup - Not returning intended values

[SOLVED] Profile Lookup - Not returning intended values
0.0 0

#1

Tell us what’s happening:

I can’t seem to understand why the first if statement is not passing as intended. I think that the issue (if there’s only one) lies with the return. Anyone giving free hints :smiley: ?

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 (contacts[i].firstName == name  && contacts[i].hasOwnProperty(prop)) {
  return contacts[i].firstName
}
  else if ( name != contacts[i].firstName) {
    return "No such contact"
  }

  else if ( prop != contacts[i].firstName || prop != contacts[i].lastName ) {
    return "No such property"
  }
}
// Only change code above this line
}

// Change these values to test your function
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/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup


#2

When you return from inside a loop, you exit the function. The conditions are set up such that you guarantee hitting a return statement on the first iteration.


#3

Hi PortableStick,

thanks for the reply but that is exactly what it’s supposed to do (technically, with the default values, it shouldn’t go through the whole loop and just stop at the first statement).

Problem is, as it is now it just returns “Akira”, and I now realise that it is because of this

return  contacts[i].firstName

Which will obviously only ever return the contact’s first name. What I need to do is return the value of the chosen property prop… and for the life of me I cannot understand how to do it. I’m sure that the hint is somewhere in the text but I can’t get it.

Edit: did a little more testing, I think I’m getting where you’re hinting at. Everything works if I use “Akira” as contact, but as soon as I choose another contact it doesn’t even get looked up. Working on it :slight_smile:


#4

Solved it :smiley: Really brutishly, but sorted. I’m sure there’s a more elegant way to go about it… but if it works :smiley:

  var output = ""
  var foundUser = 0
  for (var i = 0; i < contacts.length; i++) {
    if (foundUser == 1) {
      return output
    }
    if (contacts[i]["firstName"] == name  && contacts[i].hasOwnProperty(prop)) {
    return contacts[i][prop]
  }
    else if (name == contacts[i]["firstName"]) {
      output = "No such property"
      foundUser = 1
    } else {
      output = "No such contact"
    }
  }
  return output