Profile Lookup logic partially flawed

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(name, prop){
// Only change code below this line
var i = contacts.length;
var count =0;
do {
  var msg = "No such contact";
  if (contacts[count].firstName == name && contacts[count].hasOwnProperty(prop)){
  msg = contacts[count][prop];
  count ++;
  }
  else if (!contacts[count].hasOwnProperty(prop)) msg = "No such property";
  count ++;
}while(count < i);
return msg;
// 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 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36.

Hi all, am in need of some pointers with regards to my code logic. While it is able to iterate through the input, its not giving the right output which passes the test and i have tried picturing my code logic several times to no avail trying to figure out the which part of my code logic went wrong.
TIA

else if (!contacts[count].hasOwnProperty(prop))

This will set the msg variable even if the contact name does not match.

Dear all, based on your inputs i have restructured my code to the following:

function lookUpProfile(name, prop){
// Only change code below this line
var i = contacts.length;
var count =0;
do {
  var msg = "No such contact";
    if (contacts[count].firstName == name && contacts[count].hasOwnProperty(prop)){
    msg = contacts[count][prop];
   
    }
    else if (!contacts[count].hasOwnProperty(prop)) {
    msg = "No such property";

    }
    else
    count ++;
}while(count < i);
return msg;

However it seems just asArielLeslie has said that line of code is setting the msg variable even if the contact name doesnt match. At this point im slightly lost and have tried to resolve it by myself to noavail, apologies in advance but i may need additional pointers.

Thanks Randell, in essence i will need to break out of the if statement once the condition has been met am i right? Something like the below?

do {
    if (contacts[count].firstName == name && contacts[count].hasOwnProperty(prop)){
    msg = contacts[count][prop];
    count ++;
    break;
    }
    else if (!contacts[count].hasOwnProperty(prop)) {
    msg = "No such property";
    count++;
    break;
    }
    else{
    count ++;

    }
}while(count < i);
return msg;

Hi Randall, thanks for the pointer. I’m still trying to figure it out in my head where should i have declared the msg =“No such Contact”. Why couldn’t i declare the msg as a global variable and let that be the default state instead? So that if the do/while statement doesnt change the msg, by default it should output “No such contact”.

My revised code below:

function lookUpProfile(name, prop){
// Only change code below this line
var i = contacts.length;
var count =0;
var msg = "No such contact";

do {
  
    if (contacts[count].firstName == name && contacts[count].hasOwnProperty(prop)){
    msg = contacts[count][prop];
    break;
    }
    else if (!contacts[count].hasOwnProperty(prop)) {
    msg = "No such property";
    break;
    }
    count++;
}while(count < i);
return msg;
// Only change code above this line
}

Thank you! English isn’t my native language thus i took awhile longer to figure it out.

My final output:

function lookUpProfile(name, prop){
// Only change code below this line
var i = contacts.length;
var count =0;
var msg = "No such contact";

do {
  
    if (contacts[count].firstName == name && contacts[count].hasOwnProperty(prop)){
    msg = contacts[count][prop];
    break;
    }
    else if (!contacts[count].hasOwnProperty(prop) && contacts[count].firstName == name) {
    msg = "No such property";
    break;
    }
    count++;
}while(count < i);
return msg;
// Only change code above this line
}

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

Agreed! Thanks for pointing it out and taking the time to craft an example. Will put more thought about it for my next assignment.