Profile Lookup:Where did i go wrong

Tell us what’s happening:

Where did i go wrong i need your help.. Anyone 

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].hasOwnProperty(prop)){

        return contacts[i][prop];

      } else if (contacts[i].hasOwnProperty(prop)==false){

        return "No Such Property";

      }

    }else{

    return "No Such Contact";

    }

  }

// 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 6.1; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0.

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

Try using this to visualise your code step by step: you may find what’s wrong: http://www.pythontutor.com/javascript.html

1 Like

Your function checks the first element and then returns one of three possible responses. The other elements are never checked.

1 Like
Yes thats true. But Y is doing that.

Whenever a function encounters a return it stops the function and gives you whatever is to the right of the return. What happens when your function hits the first contact in the contacts list?

1 Like
its supposed to continue to look through the contacts and not stop
at the first contact

Okay, lets follow the logic here, okay?
let’s pass in “Akira” and “likes” per the default.
we start a loop. first stop: i === 0, "Akira" === contacts[i].firstName
Akira has the property “likes” it returns our array of likes. So far so good.

Now, let’s pass in “Harry” and “likes”.
We start the loop, now follow the logic…
What happens when i === 0?

For the record, you are very close.

1 Like

Its still gets me the first value. Which is Akira

Right. and since Harry doesn’t equal Akira, what does it do next?

It doesnt give a value

Oh no. It gives a value. What happens when the condition in an if statement is false?

it returns a value for false.Or the statement to be executed when the condition is false

Yeah, and what statement is to be executed when the condition is false?

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].hasOwnProperty(prop)) {

        return contacts[i][prop];

      } else if (contacts[i].hasOwnProperty(prop)==false) {

        return "No Such Property";

      }

    } else {

    return "No Such Contact";

    }

  }

// Only change code above this line
}
  return "No Such Contact";

this statement

There ya go. you found the problem. Now how do you think you could fix it?

the condition for false. i think. tryin to figure it out now

There’s actually nothing wrong with your logic, just your flow. Don’t think too complex. Think more about moving parts around.

This is my code so Far.
function lookUpProfile(name, prop){
// Only change code below this line
    for (var i = 0;i < contacts.length; i++){
// nested if statement
        if ( name == contacts[i].firstName){

                if(contacts[i].hasOwnProperty(prop)===true){
                    return contacts[i][prop];

                }else if (contacts[i].hasOwnProperty(prop)===false){

                    return "No Such Property";

                }
        }
// end of nested if statement
    } // end of for loop

    return "No Such Contact";
// Only change code above this line

} // end of lookupprofile function

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

this is my repl.it link

https://repl.it/@JabaHum/PungentSubmissiveCleantech

One thing that may also give you troubles (I have not checked anything else at the moment) is that you capitalise everything in the string in the return statements, but you need to have them match perfectly with the ones provided in the challenge text
"No such property" and "No such contact"

At first glance it doesn’t seem to be anything else