Contact list


#1

var search = function(name)
{
for(var key in friends)
{

if(friends[key].firstName === name)
{
    console.log(friends[key]);
    return friends[key];
}
}

};

hii couldn't understand code exactly..If it finds first name equal to our given argument, it will display that on console (using console.log)..den why are we giving "return friends[key]" here..??


#2

A good question, and wise in that it spots the irregularity. A powerful debugging skill.

There is no real why, here. It's a lesson/exercise to practice the basic mechanics and teach our fingers how to type the statements.

The exercise does illustrate the difference between console.log() and return, though, which is what I think the author may have wanted to explain but ran out of space and time. We run into loose ends like this in any introductory course. As this exercise goes, the screen output is confusing since it,

a) outputs the logged object
b) echoes the object to the console (normal command line interpreter behavior)
c) gets augmented by SCT test results and attendant interpreter responses

This leads to a lot of stuff printing to the display, when the only one that matters is the first one to print. The one we instructed the program to print. If it is correct, then we can ignore all the rest.

The difference, if anyone hasn't noticed yet is that console.log() is not a DOM instruction and cannot relate to a web page. It is a single purpose method (text output) of the Console object, which presents a terminal emulation environment only. Display, command line, interpreter/compiler, live memory, and no DOM. return however is a program directive that is intrinsic in any language, and does not have any connection to display output, but instead, program flow and data interchange.