繁体   English   中英

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

[英]trying to understand this profile lookup in JavaScript

大家好,我在理解 FreeCodeCamp 的挑战时遇到了一些问题<

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

这是我的解决方案


// 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");

我正在分享我的解决方案,这与您的解决方案略有不同。 将其与您自己的进行比较。 你会看到,当我得到一个肯定的匹配时,我只在我的 for 循环中返回,否则我让循环运行。 这是最大的不同。 您需要让循环完全运行,然后通过某种机制跟踪丢失的条件。 我在这里使用了两个不同的变量来跟踪缺失的条件。

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
}

好的,让我一步一步解释问题以及我如何解决它。

您将获得一个对象数组,其中包含在数组中的每个 object 代表一个联系人。

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

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

这是代码:

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
}

注意:我使用了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