我正在做代码学院课程的“建立联系人名单”部分。这里怎么了?继续获取错误"Oops,再试一次。看起来您的搜索功能没有返回史蒂夫的联系人信息。“(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");
}
}
};发布于 2015-06-24 16:36:47
错误出现在搜索函数中:
说明书告诉你:
定义一个函数搜索,其中包含一个参数,名称。如果传递给函数的参数与朋友中的任何名字匹配,则应该将该朋友的联系人信息记录到控制台并返回。
简而言之,它要求您创建一个函数,在该函数中,您提供要搜索的人的名称,而您提供的是friends,它也是一个全局变量。
这项运动的目的似乎是通过使用:
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");
}
}
};你就完蛋了!
http://prntscr.com/7kth5t

很好的尝试,你很接近解决方案。如果你仍然有任何问题或需要任何澄清,请随意评论。
发布于 2015-06-24 16:37:33
清单使用:
list(friends);至于搜索:
search(friends);https://stackoverflow.com/questions/31032001
复制相似问题