Need help to solve Profile Lookup exercise

Hi :grinning: The question is for 8th excercise from the end in /JS/Basic JS.

//Setup

/*The function should check if name is an actual contact's firstName and 
the given property (prop) is a property of that contact.
If both are true, then return the "value" of that property.
If name does not correspond to any contacts then return "No such contact".
If prop does not correspond to any valid properties of a contact 
found to match name then return "No such property"*/

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
    var index=0;
    var i = 0;
    while (contacts[i].firstName != name){
        i++;
        index++;
    }

    if (contacts[index].firstName === name && contacts[index].hasOwnProperty(prop)){
        return contacts[index][prop];
    }
    else if (contacts[index].firstName !== name){
        return "No such contact";
    }
    else if (contacts[index].hasOwnProperty(prop) === false){
        return "No such property";
    }
// Only change code above this line
}

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

This code (look above) passed all tests ecxept a cases, where function should return “No such contact”. Console write next message:

// running tests
Cannot read property 'firstName' of undefined
Cannot read property 'firstName' of undefined
// tests completed

I suppose the problem is in condition in the first if else.
Tell me, pls, what is wrong?

P.S. I know, that my code can be improved. But the point of this topic is why there is an error.

Your code is trying to inspect elements of contacts that don’t exist. In the tests where the contact is not in the array, your code checks the (valid) first four contacts and then attempts to check contacts[4], which is undefined.

Yea… just like ArielLeslie said. Your code is trying to access firstName of object that does not exist

Please, I have tried to crack this contacts array look up exercise, but so far not been successful. I do not understand why this code has consistently failed the test and this just tells me that I don’t understand the concept. I have read the different posts on the topic but still don’t get it. Please, could anyone possibly break it down for me?

//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) === true)) {
        return contacts[i][prop];
    } else if ((contacts[i].firstName === name) && (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");

"Kristian", "lastName" should return "Vos"
"Sherlock", "likes" should return ["Intriguing Cases", "Violin"]
"Harry","likes" should return an array
"Bob", "number" should return "No such contact"
"Bob", "potato" should return "No such contact"
"Akira", "address" should return "No such property"
262725282930313233343536373839404142434445464748
    }];        "likes": ["JavaScript", "Gaming", "Foxes"]  function lookUpProfile(name, prop){// Only change code below this linevar value;for (var i = 0; i < contacts.length; i++) {    if ((contacts[i].firstName === name) && (contacts[i].hasOwnProperty(prop) === true)) {        value = contacts[i][prop];    } else if ((contacts[i].firstName === name) && (contacts[i].hasOwnProperty(prop) === false)) {        value = "No such property";    } value = "No such contact";         }// Only change code above this line} // Change these values to test your functionlookUpProfile("Akira", "likes");
//Setup
var contacts = [
    {
        "firstName": "Akira",
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
    },
    {
        "firstName": "Harry",
…value = "No such contact";
    
 
    
}
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");
Find
No Results
// running tests
"Kristian", "lastName" should return "Vos"
"Sherlock", "likes" should return ["Intriguing Cases", "Violin"]
"Harry","likes" should return an array
"Bob", "number" should return "No such contact"
"Bob", "potato" should return "No such contact"
"Akira", "address" should return "No such property"
// tests completed

You are only checking the first value in the array because your loop is guaranteed to return something.

I’m sorry that I have just picked up your response as I took a break and headed to bed after my post.

I don’t understand what you mean by : ‘you are only checking the first value in the array’ as I had thought that the i++ tells the code to look up each element, one after the other, in the array.length index?
The truth is that I am totally lost! I am fully aware that I haven’t yet grasped the basic concept as I also was unable to solve the records collection exercise!
Kindly help extricate me from my confusion!

you have all three return statements inside the loop, the third one always execute if reached, as such when a return statement is executed a value is returned and the function stop. so, your loop is stopped at first iteration and never change the value of i

I have made corrections based on your recommendation but the code still fails to pass. Please see my code as copied and pasted below:

//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
var name = firstName;
for (var i = 0; i < contacts.length; i++) {
 contacts[i].lookUpProfile;
} 
if (contacts[i].name === firstName) {
  if (contacts[i].prop === contacts[i].prop) {
    return contacts[i];
  }
}
    else if (contacts[i].name !==  firstName) {
      return "No such contact";
    }
    else if (contacts[i].prop !== prop) {
      return "No such property";
    
    } 

// Only change code above this line
}

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

Below is the feedback I am getting:

"Kristian", "lastName" should return "Vos"
"Sherlock", "likes" should return ["Intriguing Cases", "Violin"]
"Harry","likes" should return an array
"Bob", "number" should return "No such contact"
"Bob", "potato" should return "No such contact"
"Akira", "address" should return "No such property"

Kindly help point me to the error(s).
Many thanks in advance.

I’m also getting the comments:

// running tests
firstName is not defined
firstName is not defined
firstName is not defined
firstName is not defined
firstName is not defined
firstName is not defined
// tests completed

piece by piece:

contacts[i].lookUpProfile doesn’t do anything, so the loop finish iterating without doing anything other than bringing i to value of contacts.length

"name" is not a property of contacts[i] and firstName is not defined anywhere - the function parameter is name
plus, you are making a comparison between one thing and itself, so the other if statement can just be removed as it is always true, but even that, "prop" is not a property of the object.
and then there is contacts[i], which is contacts[contacts.length] which has an undefined value - you would get error cannot read property name of undefined and cannot read property prop of undefined if there wasn’t an error before

the other parts have errors of the same kind.

the first code you posted was much better.

Thanks for your kind guidance and I will revise the code and repost my revised solution.

The code says, "FirstName is undefined " but I thought I defined this through : var name = firstName? I still don’t know how to link the function lookUp parameter (name) with the object property key (firstName).

I’ve seen many instances where the firstName was simply substituted for the name parameter which passed the test but still don’t understand why this passes.

this is how you define the variable name - which if you do this you are overwriting the value of function parameter name, and you shouldn’t overwrite the function parameters name and prop or you can’t certainly do what it is asked from the challenge as you loose passed in values
firstName is not defined anywhere.

The two parameters given for the function are name and prop, none of which are depicted in the object contacts array. I’m sorry but I don’t get this at all!

Though firstName is not defined anywhere in the function, the code running the function still accepts firstName when it is input into the if condition statement. Could you kindly explain in steps why this is the case?

can you show what code you are talking about?

name and prop are two strings passed in the function. in this case name has value of "Akira" and prop has value of "likes", which are the values you need to look up in the object. if you do var name = firstName you are losing the value passed in for name and so your function can’t do the assigned task

Thanks for your efforts at clarifying these new concepts. I have looked at the solution and have decided to leave this well alone for now. Hopefully, will return to the concept in a few days time! Many thanks for trying to help me out.