[英]contact info list not returning “error” even though it is returning the info
[英]Codeacademy contact list building 7/8… Not returning contact info
我正在做codeacademy類部分“建立一個聯系人列表”..這里有什么問題? 繼續收到錯誤“哎呀,再試一次。看起來你的搜索功能不會返回史蒂夫的聯系信息。” ( http://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/7 )
var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way', 'Redmond', 'WA', '98052']
};
friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: "(556) 555-5555",
address: ['178 martio', 'cocoa', 'CA', '95074']
};
var list = function(friends) {
for (var key in friends) {
console.log(key);
}
};
var search = function(friends) {
for (var key in friends) {
if (friends[key].firstName === "Bill" || friends[key].firstName === "Steve") {
console.log(friends[key]);
return friends[key];
} else {
console.log("couldn't find them");
}
}
};
錯誤在搜索功能中:
說明告訴你:
定義一個帶有單個參數name的函數搜索。 如果傳遞給函數的參數與朋友中的任何名字匹配,它應該將該朋友的聯系信息記錄到控制台並返回它。
簡而言之,它要求您創建一個函數,在其中提供您正在搜索的人的姓名 ,同時您提供的朋友也是全局變量 。
練習的目標似乎是通過使用:
search("steve");
你應該得到結果:
Object :
{ firstName: 'Steve',
lastName: 'Jobs',
number: '(556) 555-5555',
address: [ '178 martio', 'cocoa', 'CA', '95074' ] }
在您的(當前)搜索功能中,您將獲得的結果不是來自針(搜索參數),而是來自您自己的首選項,在if條件中定義:
if (friends[key].firstName === "Bill" || friends[key].firstName === "Steve")
因此,我們要做的是:
全部放在一起:
var search = function(name) { // <-- note the name instead of friends.
for (var key in friends) {
if (friends[key].firstName === name) { // <-- note that if
console.log(friends[key]);
return friends[key];
} else {
console.log("couldn't find them");
}
}
};
而且你已經完成了!
無論如何,好的嘗試,你非常接近解決方案。 如果您仍有任何問題或需要任何澄清,請隨時發表評論。
用於列表:
list(friends);
和搜索:
search(friends);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.