简体   繁体   English

试图了解 JavaScript 中的此配置文件查找

[英]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大家好,我在理解 FreeCodeCamp 的挑战时遇到了一些问题<

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup 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.你会看到,当我得到一个肯定的匹配时,我只在我的 for 循环中返回,否则我让循环运行。 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.您将获得一个对象数组,其中包含在数组中的每个 object 代表一个联系人。

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.每个联系人都有一些存储的数据,所以有一个由属性firstName表示的First name ,由属性lastName表示的last name ,一个由属性number表示的number ,最后一个属性是likes 。每个属性都包含一些关于联系人,因此所有联系人具有相同的属性但具有不同的值。

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.你被问到的是实现一个 function ,它带有属性: nameprop 。记住每个 object 都有一个name ,但在firstNamelastName中分开。如果在你的函数中传递的name参数验证lastName attribute either firstName属性,表示用户存在。也就是说, name参数将与firstName比较,如果相等,则表示用户存在,否则也会与lastName比较,如果相等,则表示用户存在,因此对于该用户,function 应返回名称与prop参数匹配的属性值。

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:注意:我使用了for of loop from ES6 ,但与传统 for 循环相同的代码是:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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