简体   繁体   中英

trying to understand this profile lookup in JavaScript

Hello guys I am having some issues understanding this challenge from FreeCodeCamp< i just did all the steps that I was told to do on the challange but I can just get it to work, here is the link

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

And here is my 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
for ( var i = 0; i < contacts.length; i++){

if (contacts[i].firstName == name)
{
    if (contacts[i].hasOwnProperty(prop)){
         
    return contacts[i][prop]
} else if (!contacts[i].hasOwnProperty(prop)) {
    return  "No such property"
} 
} else {
    return  "No such contact"
}

} 

// Only change code above this line
}


lookUpProfile("Harry", "likes");

I am sharing my solution which is slightly different from yours. Compare it to your own. You will see that I only return inside my for loop when I get a positive match, otherwise I let the loop run. This is the biggest difference. You need to let the loop run fully and then through some mechanism keep track of the missing conditions. I have used two different variables to track the missing conditions here.

function lookUpProfile(name, prop){
// Only change code below this line
   let hasNoName = true;
   let hasNoProp = true;
   for( let i = 0 ; i < contacts.length; i++){
       const contact = contacts[i];
       if( contact['firstName'] === name ){
           hasNoName = false;
           if( contact[prop]){
               hasNoProp = false;
               return contact[prop];
           }
       }
   }
   if( hasNoName ) return "No such contact";
   if( hasNoProp ) return "No such property";
// Only change code above this line
}

Okay, let me explain the problem step by step and how i solve it.

You are given an array of objects where each object contained in the array represents a contact.

Each contact has some data that is stored, so there is a First name represented by the attributes firstName ,the last name by the attribut lastName , a number reprented by the attribute number and the last attribute is likes .Each attribute contains some data about the contact,so all the contacts have the same attributes but with different values.

What you're asked is to implement a function that takes to attributes: name and prop .remember that each object has a name but, separated in firstName and lastName .If the name parametre passed in your fonction verifies either the lastName attribute or the firstName attribute, it means that the user exist.In another words, the name parameter will be compared with the firstName if, there are equal, it means that the user exist, otherwise it will be also compared with the lastName is they are equal, it means that the user exist, so for that user, the function shoul return the value the attributes whos name matches the prop parameter.

Here is the code:

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

//Checking whether the name parametre is equal either to the contacts firstName
// or lastName attributes
for(let contact of contacts){
    if(name===contact["firstName"] || name===contact["lastName"]){

        //Here i check if the attrbute prop exist in the object 
        if(contact[prop]){
            return contact[prop];//it return the value of that attribute if it exists
        }else{
            return "No such property" //The string that i return if it doesn't exist
        }
    }
}
return "No such contact"
// Only change code above this line
}

Notice: I've used the for of loop from ES6 , but the same code with the a traditionnale for loop would be:

function lookUpProfile(name, prop){
// Only change code below this line
for(let i=0;i<contacts.length;i++){
    if(name===contacts[i]["firstName"] || name===contacts[i]["lastName"]){
        if(contacts[i][prop]){
            return contacts[i][prop];
        }else{
            return "No such property"
        }
    }
}
return "No such contact"
// Only change code above this line
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM