7. Search for a friend: output reasoning?


#1

Hi there, first time reaching out to the community :grin:
I finally figured out how to make the code run! However, I can't quite grasp why the output repeats 'Steve's' information, show's Bill's, and then lists Steve info in a different format at the end.
Please see below!:

var friends = {
    bill: {
        firstName: "Bill", 
        lastName: "Gates", 
        number: "(909) 353-3525",
        address: ['2359 Blue st', 'Richmond', 'VA', '58364']
    },
    steve: {
        firstName:"Steve", 
        lastName: "Jobs", 
        number: "(319) 263-3005",
        address: ['2530 Happy lane', 'Nashville', 'TN', '25674']
    },
    kim: {
        firstName: "Kim", 
        lastName: "Li", 
        number: "(909) 333-5841",
        address: ['5930 Rosebud Ct', 'Chino', 'CA', '45913']
    }
};

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

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

search("Steve");

Output:

{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(319) 263-3005',
  address: [ '2530 Happy lane', 'Nashville', 'TN', '25674' ] }
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(319) 263-3005',
  address: [ '2530 Happy lane', 'Nashville', 'TN', '25674' ] }
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(319) 263-3005',
  address: [ '2530 Happy lane', 'Nashville', 'TN', '25674' ] }
{ firstName: 'Bill',
  lastName: 'Gates',
  number: '(909) 353-3525',
  address: [ '2359 Blue st', 'Richmond', 'VA', '58364' ] }
{"firstName":"Steve","lastName":"Jobs","number":"(319) 263-3005","address":["2530 Happy lane","Nashville","TN","25674"]}

Any help would be greatly appreciated!
Thank you!!


#2

This one is the output of the console.log() statement in the function:

{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(319) 263-3005',
  address: [ '2530 Happy lane', 'Nashville', 'TN', '25674' ] }

This one is the return value being sent to the display by the interpreter when your program stops.

{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(319) 263-3005',
  address: [ '2530 Happy lane', 'Nashville', 'TN', '25674' ] }

This one is the output of console.log() in the function for the SCT test run:

{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(319) 263-3005',
  address: [ '2530 Happy lane', 'Nashville', 'TN', '25674' ] }

This one is the output of console.log() in the function for the second SCT test run:

{ firstName: 'Bill',
  lastName: 'Gates',
  number: '(909) 353-3525',
  address: [ '2359 Blue st', 'Richmond', 'VA', '58364' ] }

This is a buffer dump after the SCT finishes running (or something related).

{"firstName":"Steve","lastName":"Jobs","number":"(319) 263-3005",
    "address":["2530 Happy lane","Nashville","TN","25674"]}

Now that you have passed the lesson, you can make changes that don't have to pass, but let's you test it under different conditions. Comment the line,

// search("Steve");

and Submit. What output do you see, now?

Comment the line,

// console.log(friends[key]);

and Submit. What is the output, now?

These are the only changes we can make in the current exercise. If you find a page that has no SCT (breather, first lesson, sometimes last lesson) paste the code into that page and run it. You should see only your own generated output.


#3

Perfect!
When I comment

//search("Steve");

the output is only:

{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(319) 263-3005',
  address: [ '2530 Happy lane', 'Nashville', 'TN', '25674' ] }
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(319) 263-3005',
  address: [ '2530 Happy lane', 'Nashville', 'TN', '25674' ] }
{ firstName: 'Bill',
  lastName: 'Gates',
  number: '(909) 353-3525',
  address: [ '2359 Blue st', 'Richmond', 'VA', '58364' ] }

and when I comment only

 //console.log(friends[key]);

the output is only:

{"firstName":"Steve","lastName":"Jobs","number":"(319) 263-3005","address":["2530 Happy lane","Nashville","TN","25674"]}

I should have realized the additional output from console.lot(friends[key]);.

Thank you for clarifying!


#4

What is an SCT test run?


#5

Each exercise has a program attached with specific test instructions. This is called the Submission Correction Test, or SCT for short. Some of the testing procedure may include calling the program (function) to test the results, whether on the screen or in the return value. This is a 'test run'.