Basic JavaScript: Profile Lookup

Hello, I’m learning JavaScript and my mind is going to blow up LOL because of a challenge ! :tired_face:
Link of the challenge : here

I’m trying to write a function to return :
1- “The property variable content” if the “name” and the property exist in the “contacts” array.
2- “No such contact” if the “name” doesn’t exist in the “contacts” array.
3- “No such property” if the “property” doesn’t exist in the “contacts” array.

So, this is my function :

// RETURN THE PROPERTY CONTENT 

    for(var i=0; i<=contacts.length-1; i++) {
        if (contacts[i]["firstName"] === name && contacts[i].hasOwnProperty(prop)) {
            return contacts[i][prop];
            break; 
        }
    }

    // NO SUCH NAME 

    for(var i=0; i<=contacts.length-1; i++) {
        if ((name !== contacts[i][name] && contacts[i].hasOwnProperty(prop)) || (name !== contacts[i][name] && !contacts[i].hasOwnProperty(prop))) {
            return "No such contact";
            break;
        }
    }

    // NO SUCH PROPERTY 

    for (var i = 0; i <=contacts.length-1; i++) {
        if ((contacts[i]["firstName"] === name && !contacts[i].hasOwnProperty(prop)) || (name !== contacts[i]["firstName"] && !contacts[i].hasOwnProperty(prop))) {
           return "No such property";
       }
    }

And this is the output i’m getting :

The problem is when i test my function with :

lookUpProfile("Akira", "address")

I get an unusual output, it tells me that the name “Akira” doesn’t exist in the “contacts” array.

My console after the execution of the function with the previous parameters :

// running test
"Akira", "address" should return "No such property"
// tests completed

/* MY CONSOLE IS SHOWING “No such contact” INSTEAD OF “No such property” */
THANK YOU,

1 Like

No offense, but you passed the test#4, 5 because you were lucky. You cannot test “No such contact” by doing what you wrote

name !== contacts[i][name] && contacts[i].hasOwnProperty(prop))
|| (name !== contacts[i][name] && !contacts[i].hasOwnProperty(prop)

Error #1: contacts[i][name]
contacts can’t be accessed using name variable. Because that’s the actual first name, rather than the property, "firstName".

Suppose you fixed that to "firstName
Error #2: the conditions you wrote can’t tell you “No such contact”
Your condition translates to

  • Person doesn’t exist in given contact but property does OR
  • Both the person and property doesn’t exist in given contact.

I can only say this doesn’t make any sense when you want to know “No such contact”

The only way to figure this out is that you checked every contact and you couldn’t find the person. So, you cannot return within your loop because that means not every contact has been explored. Also, prop has nothing to do with this case.

Error #3: the condition you wrote can’t tell you “No such property”
The reason is similar to error#2. If you are returning within the loop, that means you haven’t checked all the contacts.
The correct logic for this one is

  • You searched a person in the contact
  • but you couldn’t find the given property

Redundancy #1: you don’t need break
break is right below on every return statement; but, return ends the function. This means loop won’t continue. So, ‘break’ is unnecessary.

So, I’d suggest you to rethink about the problem and come back if you still can’t figure out.
Feel free to ask anything that is unclear about what I’ve said

2 Likes

I modified the loop of “No such contact” :

var check = false;
    for(var i=0; i<=contacts.length-1; i++) {
        if (contacts[i]["firstName"] === name) {
            check = true;
        } 
    }
    if(!check) {
        return "No such contact";
    } 

So, my function is like this :

function lookUpProfile(name, prop) {
    // Only change code below this line
    

    // RETURN THE PROPERTY CONTENT 

    for(var i=0; i<=contacts.length-1; i++) {
        if (contacts[i]["firstName"] === name && contacts[i].hasOwnProperty(prop)) {
            return contacts[i][prop];
        }
    }

    // NO SUCH NAME 

    var check = false;
    for(var i=0; i<=contacts.length-1; i++) {
        if (contacts[i]["firstName"] === name) {
            check = true;
        } 
    }
    if(!check) {
        return "No such contact";
    } 
    // NO SUCH PROPERTY 

    for (var i = 0; i <=contacts.length-1; i++) {
        if (!contacts[i].hasOwnProperty(prop)) {
           return "No such property";
       }
    }
    

    // Only change code above this line
}

AND MY CODE IS WORKING NOW, I PASSED THE CHALLENGE !
THANKS FOR YOUR QUICK RESPONSE, IT WAS VERY HELPFUL :smiley:

1 Like

Also, you can push yourself to use one loop instead of three loops.

1 Like

This is my final code :

function lookUpProfile(name, prop) {

    var checkContact = false;
    var checkProperty = false;

    for(var i=0; i<=contacts.length-1; i++) {

        // RETURN THE PROPERTY CONTENT 
        if (contacts[i]["firstName"] === name && contacts[i].hasOwnProperty(prop)) {
            return contacts[i][prop];
        } else 
        // NO SUCH CONTACT 
        if (contacts[i]["firstName"] === name) {
            checkContact = true;
        } else  
        // NO SUCH PROPERTY 
        if (!contacts[i].hasOwnProperty(prop)) {
           checkProperty = true;
        }
    }

    // CHEKCS
    if (!checkContact) {
        return "No such contact";
    } 
    if(checkProperty) {
        return "No such property"
    }
}

Thank You, and I’m sorry for my mistake, I’m a newbie here.

Similar code, simplified logic

function lookUpProfile(name, prop){
    for (var i = 0; i < contacts.length; ++i) {
        if (name === contacts[i]["firstName"]) {
            if (contacts[i].hasOwnProperty(prop)) {
                return contacts[i][prop]
            }
            return "No such property"
        }
    }
    return "No such contact"
}
1 Like