Profile Lookup, hey saw the solution but was wondering what i swrong with my code i kinda think it should work

Profile Lookup, hey saw the solution but was wondering what i swrong with my code i kinda think it should work
0

#1

Tell us what’s happening:

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(firstName, prop){
// Only change code below this line
for (w=0; w < contacts.length; w++){
  if (contacts[w].firstName === firstName && contacts[w].hasOwnValue(prop)){
    return contacts[w][prop];
  }else if (contacts[w].firstName === firstName){
    return "No such contact";
} else {
  return "No such property";
}
}

// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Harry", "likes");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36.

Link to the challenge:


#2

The problem with your logic is that it only makes it one iteration through the for loop before return a value. Once a return statement is executed, the value specified is return and the function is exited. The for loop stops once the function is exited.


#3

Ok thanks, Using the same logic what do i need to change/modify/add/remove for it to work…or is it completely impossible to attack the problem from my(logical) angle


#4

First, you need the correct algorithm/steps and then you write the code that performs those logic steps.

You need to think about when/where each of these return statements makes sense to occur.

For example, when would you want to return “No such contact”? I think you would not want to return “No such contact” until you have checked every contact’s firstName property for a match of the firstName argument. If you do not get a match, then you know you must return “No such contact”.

You have the general logic laid out with respect to iterating through the contacts array of objects. You just need to figure out when/where you should be performing certain checks.


#5

Thanks A lot i understand now…