PROFILE LOOKUP PROBLEM (Undefined Problem)

Hi guys, can you please help me figure out what is causing the issue in my code to not execute properly. (PS: I did not look at the solution yet, and I do not want to look at it until I can get it done in my own way first)

The below code outputs undefined


// 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
while (name !== nameLook) {
    var a = 0;
    var nameLook = contacts[a].firstName;
    var propLook = contacts[a].hasOwnProperty(prop);
    a ++;
};
if (name === nameLook && propLook === true){
    return contacts[a].prop;
}       else if (name !== nameLook){
        return "No such contact";
}       else if (propLook === false){
        return "No such property";
}       else {
        return contacts[a].prop;
};


// Only change code above this line
}

console.log(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/86.0.4240.193 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

You’ve got a logic error with your while loop. You set the variable a to 0 every time the loop begins and then you increment it at the end of the loop, but this does nothing since you then set it to 0 again at the start of the next loop. Thus, the while loop condition name !== nameLook will never change (because you are always looking at contacts[0]) and you’ll get an infinite loop, which if you look closely at the output is exactly the error messages you will see:

Potential infinite loop detected on line 33. Tests may be failing because of this.

Also, if you are going to use a while loop here then you need to check if you have run out of contacts to check so you can break out of the loop. Generally, if you are iterating through an array you would use some other type of loop, such as a for loop.

1 Like

Hi @mekGlitch
The variable "nameLook " is undefined at the beginning inside the while condition. I didn’t run your code but I think the “if” condition should be included inside the while loop. otherwise it will generate wrong outputs.

It is better to proceed with for loop. This loop will iterate through objects in contacts array. Then check for relevant conditions and generate relevant outputs.

for(let i=0; i < contacts.length; i++){

    /* check for relevant conditions
        and try to return correct outputs  */

}

If you prefer to proceed with while loop, this might be applied.

let i = 0;
while(i < contacts.length){

  // check conditions and return outputs

}
1 Like

Hi Guys, thanks for the replies. I used your advice and instead used a “for” loop as well as nesting the “if” statement in the loop. And I broke the loop once it found what is was looking for OR not looking for

The below code passes the tests, it’s quite similar to the proposed solution


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

// Only change code above this line
}