开发者问题收集

这段 javascript 代码有什么错误?

2013-11-17
1953

编辑:codeacademy 的说明:

Define a function search that takes a single argument, name. If the argument passed to the function matches any of the first names in friends, it should log that friend's contact information to the console and return it.

我正在通过 code academy 学习 JavaScript。我正在编写一个简单的通讯录,其中包含好友的详细信息,并且有一个搜索功能可以获取联系人的详细信息/这是代码:

var friends = {
    bill: {},
    steve: {}
}

friends.bill.firstName = "bill";
friends.bill.lastName = "gates";
friends.bill.number = 587678;
friends.bill.address = ['mes quarters', 'east camp'];

friends.steve.firstName = "steve";
friends.steve.lastName =  "Jobs";
friends.steve.number = 67896986;
friends.steve.address = ["nch colony", "kanjur marg"];

var list = function() {
    for(var friend in friends){
       // console.log(friend);
    }
} 

var search = function(name){
    for(var prop in friends)
    if(name === friends[prop].firstName){
        console.log(friends[prop]);
        return friends[prop];
    }
}

list();
search("bill");
search("steve");

此代码在 codeacademy 系统中不被接受为正确代码。它显示以下错误:

Oops, try again! It looks like your search function doesn't return contact information for Steve.

(Codeacademy 多次未显示正确的错误)

我在 Codeacademy 论坛上发布了此信息并寻求帮助。我得到了一个回复:

To answer the question, because your data structure is messed up. Go back into the lesson to get it right:

It's like this:

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: "(408) 555-5555",
address: ['1 Infinite Loop','Cupertino','CA','95014']
};

有人可以解释一下,我的代码到底有什么问题吗?正确的方法是什么?以下是我的问题的 codeacademy 论坛链接: http://www.codecademy.com/forum_questions/52883d7c548c358401006826#answer-5288c908abf821c7450089fc

3个回答

这只是为了通过 Codeacademy 测试,还是为了制定适当的通讯录?如果是后者,请使用类:

对于应用程序

"use strict";
var Contact = function(fName, lName, number, address) {
  this.firstName = fName;
  this.lastName = lName;
  this.number = number;
  this.address = address;
};
var contacts = [];

然后,要添加联系人,请使用:

function addContact(fn, ln, nm, ad) {
  contacts.push(new Contact(fn, ln, nm, ad));
}

addContact("Bill", "Gates", "(206) 555-5555", ['One Microsoft Way','Redmond','WA','98052']);

并搜索:

function searchContact(query) {
  for(var x in contacts) {
    if(contacts[x].firstName == query) {
      return contacts[x];
    }
    else
      return false;
  }
}

对于测试

但是,如果您只想通过这项测试,我猜您应该模仿练习中的代码。验证器脚本可能会检查您的姓名 ==“Bill”“Steve” - 这意味着您使用的字符串 “bill”“steve” 因区分大小写而永远不会通过测试。

该测试旨在帮助您学习编程,因此请尝试研究代码,直到您发现您所做的事情与实际工作之间存在显著差异!祝您好运。

gchiconi
2013-11-17
var friends= {};
friends.bill={
    firstName:"Bill",
    lastName:"Gates",
    number:"9966334418",
    address: ['One Microsoft Way','Redmond','WA','98052']
    };

friends.steve={
    firstName:"Steve",
    lastName:"Jobs",
    number:"9966224418",
    address:['banglore',]
    };
   var list = function(obj) {
  for(var prop in obj) {
    console.log(prop);
  }
};

var search = function(name) {
  for(var prop in friends) {
    if(friends[prop].firstName === name) {
      console.log(friends[prop]);
      return friends[prop];

    }
  }
};

list(friends);
search("Steve");
Rohit
2014-03-15

我也遇到了同样的事情,但我怀疑这是 CodeAcademy 的一个错误,因为在能够显示 Steve 的联系信息后,我收到了这个错误:“哎呀,请再试一次。看来您的搜索功能没有返回 Bill 的联系信息”。

Felin
2016-02-15