Correct but Printed Twice


#1

Continuing the discussion from 6. List em all - Not sure I understand this?:

Thank you @mtf, respone to @awalklearn (8 days ago) helped me fix this one, at least mostly - was marked correct, but printed their names twice, don't know why -

bill
steve
bill
steve

Code:

var friends = new Object();

friends.bill = new Object();
friends.bill.firstName = "bill",
friends.bill.lastName = "Gates",
friends.bill.number = "(206) 555-5555",
friends.bill.address = ['One Microsoft Way','Redmond','WA','98052'];

friends.steve = new Object();
friends.steve.firstName = "steve",
friends.steve.lastName = "jobs",
friends.steve.number = "(408) 555-5555",
friends.steve.address = ['1 Infinite Loop','Cupertino','CA','99999'];

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


#2

@manhattanalchemist,

++++ why second display of last Item
+++++++++++++++++++++++++++++
-1
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....
-2
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 ==" );


#3

Thanks for that clarity, interesting. Thought it might have been something deeper that I didn't understand yet, since the last items typed had printed in a few exercises right before this one, even though no console.log's were used.

A lot more to learn (& taking detailed notes), thanks again,

Robert.


#4

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()..


#5

I see, an addition to the notes then, thank you. :relaxed:


#6

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 [ return is implicit ])

Example of #3...

console.log(console.log("This function has no return value."));

Output

This function has no return value.
undefined

#7

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.


#8

A little bit of promotion here: you can try using this bookmarklet to view the actual SCT code of an exercise.


#9

In a sense this is moot, today, as the composer courses are scheduled to come down this summer (afaik).