Does this code do what it states? I think not!


#1

I have been deliberating over this code for at least 4 hours.

It is part of an exercise that I am working on. I am no coding pro, but this simply does not return what I should think it should which is just either Bill or Steve's details.

In the coding error it highlighted that the code should only have printed Steve's detail's, But I wanted to print Bill's. What it actually returned was two instances of Steve's contact details, one instance of Bill's contact details, and finally another instance of Steve's contact details.

Call me potty - NOT my code; this is the sample code supplied by Codecademy, but how can this be right according to the Codecademy results checker.

Somebody provide some incites.


var friends = {};
friends.bill = {
    firstName: "Bill",
    lastName: "McGuire",
    number: "023 4536254",
    address: ['32 Lala Loppy Street' , 'Derryville', 'Lunchbox'],
};
friends.steve = {
    firstName: "Steve",
    lastName: "Artworth",
    number: "023 8472038",
    address: ['67 Quirky Lane', 'Derrymanville', 'Lunchbox'],
 
};

var list = function (book) {
    for( var key in book ) {
        console.log(key);
    }
}

var name = prompt("Enter Steve or Bill");
var search = function(name) {
  for(var key in friends) {
    if(friends[key].firstName === name) {
        console.log(friends[key]);
        return friends[key];
    }
  }
};


#2

the codecademy result checker actually calls the function for validation purpose, the output of these function calls is displayed because of console.log()

try it on jsbin, there it works fine

Remember: functions only execute when called


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.