7/8 Output not as expected - but system says code is right?!


#1

Hi everybody,

Please can you take a look at this for me? Here's my code:

var friends = {

bill: {
firstName: "Bill",
lastName: "Jones",
number: "(0114)2123456",
address: ["1 High Street", "Somewhere", "S20 1XX"]},

steve: {
firstName: "Steve",
lastName: "Smith",
number: "(0114)2419776",
address: ["1 Main Road", "Anywhere", "S12 4ZZ"]}
};

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

search ("Steve");

Which the system says is OK, but here's the output in the console:

{ firstName: 'Steve',
lastName: 'Smith',
number: '(0114)2419776',
address: [ '1 Main Road', 'Anywhere', 'S12 4ZZ' ] }

{ firstName: 'Steve',
lastName: 'Smith',
number: '(0114)2419776',
address: [ '1 Main Road', 'Anywhere', 'S12 4ZZ' ] }

{ firstName: 'Steve',
lastName: 'Smith',
number: '(0114)2419776',
address: [ '1 Main Road', 'Anywhere', 'S12 4ZZ' ] }

{ firstName: 'Bill',
lastName: 'Jones',
number: '(0114)2123456',
address: [ '1 High Street', 'Somewhere', 'S20 1XX' ] }

{"firstName":"Steve","lastName":"Smith","number":"(0114)2419776","address":["1 Main Road","Anywhere","S12 4ZZ"]}

Surely it should just output Steve's contact info, and only once? Why is it giving me all of this rubbish? Is there anything I can (should) do to stop it?

I'm confused!

Thanks for your time.

Annetta.


#2
var friends = { 

bill: {
 firstName: "Bill",
 lastName: "Jones",
 number: "(0114)2123456",
 address: ["1 High Street", "Somewhere", "S20 1XX"]}, 

steve: {
 firstName: "Steve",
 lastName: "Smith",
 number: "(0114)2419776",
 address: ["1 Main Road", "Anywhere", "S12 4ZZ"]} 
};

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

search ("Steve");
console.log("== My code END ==");
console.log("== all what follows, is the code-checker ==");

#3

This worked for me. Thank you.

I would like to understand this a little bit better. Is this happening because the developer who wrote the code checker for this lesson included some console.log debug output that was never removed or cleaned up?


#4

@kimball_lewisgmail_c,
A lot of the people only write the function-definition
without actually calling the function....
and the Javascript interpreter itself is giving the last-done statement which doesnot use a return...

More Important is,
that you must read the instructions, as if it where the specifications of a client
and try to think of ways to TEST
if the specification was properly implemented...


#5

Excuse me, but what lines in your code define the code-checker?


#6

@qblock,
By using...

you can determine,
which output is yours,
and which eventual output is caused by the code-checker and/or Javascript-interpreter.


#7

But these are comments, how did you realize those things that follow it?
For example, when I try your code ending in ex. 7 it gives me:
As I suppose that under these comments is actually that code checker, which shows us the rest of the code.
But when I use the same code in ex. 8 it gives:
Maybe I understood sth in a wrong way.
Could you tell me please what is the difference and why does it happen?
I'm a newbie and just starting to get these things.


#8

@qblock,

For ex-7

Search for a friend,"test_functions":

"try {
search(\"Steve\");
}
catch(ReferenceError) {
return \"Did you create a function called search?\";

if (typeof(search) !== 'function') {
return \"It looks like search isn't a function.\";
}

var steve_search = search(\"Steve\");
var bill_search = search(\"Bill\");

if (!(steve_search instanceof Object)) {
return \"It looks like your search function doesn't return contact information for Steve.\";
}
if (!(bill_search instanceof Object)) {
return \"It looks like your search function doesn't return contact information for Bill.\";
}

if (CC.prints().length === 0) {
return \"It looks like you didn't log anything to the console.\";
}

if (error) {
return \"It looks like you have an error in your code! Check the message for more details: \" + error;
}

return true;
","completed"

ex 8

Victory!,"test_functions":"return true;\n","completed":true


#9

I changed my code to this and it looks better:

var search = function (name) {
for (var key in friends) {
if (friends[key].firstName === name) {
console.log("First Name:" + " " + friends[key].firstName);
console.log("Last name:" + " " + friends[key].lastName);
console.log("Number:" + " " + friends[key].number);
console.log("Address:" + " " + friends[key].address);
;
}
}
};

search("Steve");

Hopefully this works for you.


#10

Thank you for help, I got it:)


#11

thx, man. I appreciate your help, guys