My code is nearly the same as code academy's example but output is different, why?


#1

The system accepts my code but the output is different than the code that code academy gives as an example at the beginning of the lesson. My code prints out duplicates of Steve's info, and also lists Bill's, though I don't understand why. I've compared my code with the example at the beginning of the lesson but other than phone numbers and addresses being different, I'm not seeing any noticeable difference between the two sets of code that point to the issue. Can anyone see what I'm doing wrong?

Thanks


var friends = {};
friends.bill = {
    firstName: "Bill",
    lastName: "Gates",
    number: "(456) 444-5656",
    address: ['345 Dogwood Ln', 'Cupertino', 'CA', '45654']
};
friends.steve = {
    firstName: "Steve",
    lastName: "Jobs",
    number: "(555) 555-5555",
    address: ['5465 Barberry Drive', 'Buford', 'GA', '34545']
};

var list = function(object) {
    for(var prop in object) {
        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");


#2

Last two lines..

list(friends);

List takes items of friends Array and print their first name.
so you see

bill
steve

Now this line..

search("Steve");

Can you see that inside your search function,You're returning the value and also printing it?

console.log(friends[prop]);
return friends[prop];

so you see steve (Object) gets printed twice.`


#3

At the bottom of this comment is the code that code academy gives at the beginning of the lesson. When you run their code you get:

bill
steve
{ firstName: 'Steve',
lastName: 'Jobs',
number: '(408) 555-5555',
address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{"firstName":"Steve","lastName":"Jobs","number":"(408) 555-5555","address":["1 Infinite Loop","Cupertino","CA","95014"]}

I don't see what I'm doing different than them. When I run my code I get:

bill
steve
{ firstName: 'Steve',
lastName: 'Jobs',
number: '(555) 555-5555',
address: [ '5465 Barberry Drive', 'Buford', 'GA', '34545' ] }
{ firstName: 'Steve',
lastName: 'Jobs',
number: '(555) 555-5555',
address: [ '5465 Barberry Drive', 'Buford', 'GA', '34545' ] }
{ firstName: 'Steve',
lastName: 'Jobs',
number: '(555) 555-5555',
address: [ '5465 Barberry Drive', 'Buford', 'GA', '34545' ] }
{ firstName: 'Bill',
lastName: 'Gates',
number: '(456) 444-5656',
address: [ '345 Dogwood Ln', 'Cupertino', 'CA', '45654' ] }
{"firstName":"Steve","lastName":"Jobs","number":"(555) 555-5555","address":["5465 Barberry Drive","Buford","GA","34545"]}

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

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

#4

Hi @fullyard, Have you read my post?
I tested your code and Im getting this output

bill
steve
{ firstName: 'Steve',
lastName: 'Jobs',
number: '(408) 555-5555',
address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{"firstName":"Steve","lastName":"Jobs","number":"(408) 555-5555","address":["1 Infinite Loop","Cupertino","CA","95014"]}

here snap of my test..(of your code)


#5

Hi Green, I saw your comment. The odd thing though is that I run the same thing on my end and get a different result (see below). It's almost as though there is a bug in the console.


#6

Probable reason: CC interpreter providing input to it and testing .
(It does in most of the cases)


#7

Thanks Green. I was thinking that it was probably something on the other side causing the issue. Just wanted to get confirmation that I wasn't going crazy. I work for a SaaS company (sales, not programming), and am pretty good at spotting bugs and other issues. I was pretty sure it wasn't me but just wanted to make sure.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.