2016-07-03 95 views
0

我嘗試通過配置文件查找來完成任務。它應該搜索道具並返回他的價值,如果它不能,那麼從條件 的「沒有這樣的接觸」或「沒有這樣的屬性」依賴我不明白爲什麼最後一個條件(else語句)不執行。這是代碼。我被困在循環任務中

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(firstName, prop) { 
    var i = 0; 
    while (contacts[i]) { 
     if (contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop)) { 
      return contacts[i][prop]; 

     } 
     i++; 
    } 
    if (!contacts.firstName) { 
     return "No such contact"; 
    } 
    else { 
     return "No such property"; 
    } 

} 

lookUpProfile("Akira", "address");` 

它返回「沒有這樣的聯繫」,雖然應該返回「沒有這樣的屬性」。

+1

'接觸' 是一個數組,也沒有 '的firstName'! – MHS

回答

0

如果找到名稱,您可以保存一個標誌來表示。 像這樣:

function lookUpProfile(firstName, prop) { 
    var i = 0; 
    var nameflag = false; 
    while (contacts[i]) { 
     if (contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop)) { 
      return contacts[i][prop]; 
     } 
     if (contacts[i].firstName == firstname) 
      nameflag = true; 
     i++; 
    } 
    if (!nameflag) { 
     return "No such contact"; 
    } 
    else { 
     return "No such property"; 
    } 

} 
0

if (!contacts.firstName)始終爲false

+0

它必須在評論中 – nmnsud

0

這是你的功能應該是什麼樣子

function lookUpProfile(firstName, prop){ 
var i = 0; 
var a; 
while(contacts[i]) { 

if (contacts[i]["firstName"] === firstName && contacts[i].hasOwnProperty(prop))  { 
    return contacts[i][prop]; 

} 
else if (contacts[i]["firstName"] != firstName) { 
    a = "No Contact"; 
} else { 
    a= "No property"; 
    return a; 
} 

i++; 
} 
return a; 

} 
0

我已經更新了你的腳本,它返回No such property

function lookUpProfile(firstName, prop){ 
    var i = 0, index = 0; 
    while(i < contacts.length) { 
     if (contacts[i].firstName == firstName) { 
      index++; 
     } 
     i++; 
    } 
    if (index > 0) { 
     if (!contacts[index][prop]) { 
      return "No such property"; 
     } 
    } else { 
     return "No such contact"; 
    } 
} 
console.log(lookUpProfile("Akira", "address")); //Output: No such property 
console.log(lookUpProfile("John", "address")); //Output: No such contacts 

希望也適合你。

1

你可以通過contacts循環,如果匹配的名字接觸被發現,你需要返回屬性的任一值或「沒有這樣的財產」。如果循環結束而沒有返回任何值,則意味着未找到聯繫人。

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

JSBin:https://jsbin.com/pobunavacu/edit?js,console

編輯:(ES6版)

如果你寫的沒有IE或Opera控制環境的代碼,你可以用ES6功能做到這一點更容易的Javascript(在Chrome,Firefox和NodeJS中測試):

function lookUpProfile(firstName, prop) { 
    return (contacts.find(contact => contact.firstName === firstName) || {[prop]: "No such contact"})[prop] || "No such property"; 
} 
0

您需要標記(布爾),您的問題

function lookUpProfile(firstName, prop) { 
var contactExist = false; 
var i = 0; 
while (contacts[i]) { 
    if (contacts[i].firstName === firstName) { 
     if (contacts[i].hasOwnProperty(prop)) { 
      return contacts[i][prop]; 
     } else { 
      contactExist = true; 
      break; 
     } 
    } 
    i++; 
} 

if (!contactExist) { 
    return "No such contact"; 
} 
else { 
    return "No such property"; 
} 

}