简体   繁体   English

此JavaScript代码有什么问题?

[英]whats wrong in this javascript code?

EDIT : Instruction from codeacademy : 编辑:来自代码学院的说明:

Define a function search that takes a single argument, name. 定义一个使用单个参数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. 如果传递给该函数的参数与好友中的任何名字匹配,则应将该好友的联系信息记录到控制台并返回。

I am learning JavaScript through code academy. 我正在通过代码学院学习JavaScript。 I am coding for a simple contact book , which have details of friends and there is asearch function to get the details of the contact/ Here is the code : 我正在编写一本简单的通讯录,其中包含朋友的详细信息,并且具有搜索功能以获取联系人的详细信息/这是代码:

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");

This code is not accepted as correct in the codeacademy system. 在代码学院系统中,该代码不被接受为正确的代码。 It shows following error : 它显示以下错误:

Oops, try again! 糟糕,再试一次! It looks like your search function doesn't return contact information for Steve. 您的搜索功能似乎未返回Steve的联系信息。

(Codeacademy do not show correct errors many times) (Codeacademy多次未显示正确的错误)

I posted this in the Codeacademy forum and asked for help. 我将其发布在Codeacademy论坛上并寻求帮助。 I got one reply : 我得到一个答复:

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'] }; 

Can anyone explain me , what exactly wrong in my code? 谁能解释我,我的代码到底有什么问题? and what is the correct way? 正确的方法是什么? Here is the codeacademy forum link for my question : http://www.codecademy.com/forum_questions/52883d7c548c358401006826#answer-5288c908abf821c7450089fc 这是我的问题的codeacademy论坛链接: http : //www.codecademy.com/forum_questions/52883d7c548c358401006826#answer-5288c908abf821c7450089fc

Is this just for passing the Codeacademy test, or for working out a proper contact book? 这是仅用于通过Codeacademy测试,还是用于制定适当的通讯录? If it's the latter case, use a class: 如果是后一种情况,请使用一个类:

For an app 对于应用

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

And then, to add contacts, use: 然后,要添加联系人,请使用:

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']);

And to search: 并搜索:

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

For the test 为了测试

However, if you mean to pass this test only, my guess is that you should mimic the code from the exercise. 但是,如果您只想通过此测试,我想您应该模仿练习中的代码。 The validator script probably checks if your names == "Bill" and "Steve" - meaning the strings you used, "bill" and "steve" would never pass the test due to case sensitivity. 验证程序脚本可能会检查您的名称是否== "Bill""Steve" -表示您使用的字符串"bill""steve"由于区分大小写而无法通过测试。

The test is supposed to help you learn programming, so try to study the code until you find the significant difference from what you've done and what works! 该测试应该可以帮助您学习编程,因此,请尝试研究代码,直到发现与已完成的工作和有效的工作有显着差异为止! Best of luck. 祝你好运。

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");

同样的事情发生在我身上,尽管我怀疑这是CodeAcademy的一个错误,原因是在能够显示Steve的联系信息之后,我遇到了以下错误:“糟糕,再试一次。看起来您的搜索功能没有返回Bill的联系信息”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM