1

EDIT : Instruction from 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.

I am learning JavaScript through code academy. 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.

(Codeacademy do not show correct errors many times)

I posted this in the Codeacademy forum and asked for help. 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

16
  • 1
    You've got his address and phone number wrong... Commented Nov 17, 2013 at 15:22
  • 1
    Maybe it's case sensitive? You aren't returning information for Steve, you are returning information for steve. Commented Nov 17, 2013 at 15:33
  • 2
    It was the problem with Codeacademy verification system. I changed bill to Bill and steve to Steve in the first name and searched for Bill and Steve instead of bill and steve respectively, the code passed through the system. Commented Nov 17, 2013 at 15:37
  • 1
    I would say this isn't a problem with their system... it's what we mentioned before - it's case sensitive, as is most things when programming. Just something you should be aware of... searching for steve is NOT the same as searching for Steve. Commented Nov 17, 2013 at 15:38
  • 2
    @Oriol: Please don't confuse beginners with unnecessarily broad constraints. Instead of telling them to "always" do that, tell them when it makes sense, when it doesn't, and the downsides each way. Commented Nov 17, 2013 at 15:40

3 Answers 3

2

Is this just for passing the Codeacademy test, or for working out a proper contact book? 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.

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.

Sign up to request clarification or add additional context in comments.

2 Comments

-1: == is case sensitive, like ===. E.g. 'bill' != 'Bill'
Fixed - either way, what I said still holds; "bill" is different from "Bill" in the site's exercise checker. Fixed the == false statement, though. Thanks
1
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");

1 Comment

good answers provide explanation, not just some code. Also, is there anything you find insufficient in the other answer that motivated you to post your own?
1

Same thing happens to me, though I suspect it's a bug on CodeAcademy's end, owing to the fact that after being able to display Steve's contact information, I got this error: "Oops, try again. It looks like your search function doesn't return contact information for Bill".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.