[英]trying to understand this profile lookup in JavaScript
大家好,我在理解 FreeCodeCamp 的挑戰時遇到了一些問題<
這是我的解決方案
// 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 ,它帶有屬性: name
和prop
。記住每個 object 都有一個name
,但在firstName
和lastName
中分開。如果在你的函數中傳遞的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.