4. Console logging the names twice?


#1
var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
    return(person.firstName + " " + person.lastName);
}

var list = function() {
    for (var i = 0; i < contacts.length; i++) {
        console.log(printPerson(contacts[i]))
    }
};

list()

So this is my code and it passes (yipee!) but I don't understand why the console prints Bob Jones and Mary Johnson twice, like this:

Bob Jones
Mary Johnson
Bob Jones
Mary Johnson

I only called the list function once. Can anyone tell my why it ends up printing twice and how to make it print only once? If this was a practical application, it would make more sense for an address book to only list the person's name once.


#2

First - thank u, u helped me get this one.

Second - the basic answer is - don't worry about it - I had asked the same question and that's what I was (basically) told - the multiple-printings are just a function of the system's Course-Checker.

If you would like the detailed explanations I was given, I have them in my notes, just reply here and let me know if you want them.


#3

Yes, please! I'd love a complete explanation if it's not too much trouble?


#4

These were the answers I received about it, from two Posters, (& I don't precisely understand every detail involved yet, but I'm just starting, if you'd like more clarity on anything here, you can find both of these Posters in many spots in the forum & send them a question, or you can try me first & I'll give it a shot):

leonhard_wettengmx_n -----

@manhattanalchemist,

++++ why second display of last Item
+++++++++++++++++++++++++++++
It is the Javascript interpreter which is running in this course-environment,
it display's the last non-displayed action...

You will also encounter this, when you are doing a return-statement
as last action...
You could counter this by using a
console.log( " " ); as last statement....

In some sections of this course, the course-checker Display's data
this you could then identify by placing to EXTRA code-line's:
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );

mtf Codecademy Moderator -----

It's actually the SCT (Submission Correctness Test) that is causing the second print out, not the interpreter, though what @leonhard_wettengmx_n explains is also true in cases where the last executed statement is NOT a console.log()..

mtf Codecademy Moderator -----

Remember, we are in the console environment which is a terminal and command line with a JavaScript interpreter/compiler, but no document tree. The natural behavior of a terminal is to respond to all commands, even if the response is undefined. Response can take three basic forms:

1.as descibed by Leon, above; (log() is not last command)
2.as described by me, above; (log() is last command)
3.undefined (last command in a function with no return value)

Example of #3...
console.log(console.log("This function has no return value."));

Output
This function has no return value.
Undefined

mtf Codecademy Moderator -----

Getting around the SCT is pointless, but we can see what the SCT is doing (in some cases) by turning off all generated output and running the code. Then creep it in and test as you go. When double output appears, we have a good indication of how the lesson checker is looking at our work.

There are a lot a different ways for the SCT to check lessons. The various authors who worked independently all chose their own approach, which adds to the dimensionality. It is a broad spectrum.

Essentially, we can break it down to three types of tests, each of which may have their own sub-test.
1.Code typed into the editor. The SCT can see this and do string or pattern matches.
2.Output printed to the display. The SCT can read the screen of the terminal and do string or pattern matches.
3.Return values from executed functions. The SCT will call the function with an argument of the author's choosing and examine the return value.

By knowing this, we can play with our code to get a read on the SCT. It's purely for fun, mind. Not to prove anything.


#5

It's because you used:

list()

afterwards, which if you look at your function it prints the contacts within the list. So basically by removing the "list()" at the bottom, despite what the instructions say, you will only have the names printed once.

I don't know if that made any sense, so I apologize!

Edit (10:47 PM EST):

I also want to point out in my code (which passed, although yours did too, but I'll show you anyways):

var list = function()
{   
    for (i = 0; i < contacts.length; i++)
    {
       ** printPerson(contacts[i]);**
    }
};

(excuse the lack of indentation)

Instead of using:

console.log(printPerson(contacts[i]))

I instead used the function that we had made in the previous lesson. It appears that both ways work, otherwise you wouldn't have passed of course, but I figured I would just point out that I used the function that we had previously made; actually made use of the function we made.


#6

You can verify that the code is correct by pasting it into a JS Fiddle, and opening the inspector console. The names will only print out once each, not twice.

Also, just a tip: I would put the console.log in the printPerson function, since that what the function is supposedly doing, and then simply call that function in the list for loop, to initialize the console.log.

Like so:

function printPerson(person) {
    console.log( person.firstName + " " + person.lastName );
};
function list() {
    var contactsLength = contacts.length;

    for(var i = 0; i < contactsLength; i++) {
        printPerson(contacts[i])
    };
};

list();

#7

Exactly. It is also known as SCT (submission correctness test), and it will call your function and test the return value. My bookmarklet allows you (on some of the exercises) to read the SCT code.