1

I am writing a search function within my Javascript code. However, it keeps on returning search function doesn't return correct contact information.

The goal of this search function is to return the contact information of the object , ie, firstName , lastName , number and address. If given an input Name , it matches one of the keys in the object I created, it should return the contact information and log it on the console.

var friends = {
    bill:{
        firstName : "Bill",
        lastName : "Gates",
        number : "(123) 456- 7890", 
        address : ['Microsoft', 20]
        },
    steve:{
        firstName : "Steve",
        lastName : "Jobs",
        number : "(123) 456- 7890",
        address : ['Apple', 30]
        }      
};
var friends1 = new Object();
var list =  function(friends1){
    for(var key in friends1){
        console.log(friends1[key].firstName.toLowerCase());   
    }  
};

var search = function(name){
 for(var key in friends1){
     if(friends1[key].firstName.toLowerCase() === name.toLowerCase()){
        console.log(friends1[key]);
        return friends1[key];
        }
 }   
};
list(friends1);
search("Steve");
2
  • 1
    Almost regardless what environment you're running your JavaScript in, there's a fully-featured debugger available that you can use to step through your code statement-by-statement, examine variables, etc. Using that is by far your best best for solving issues like this. In a browser, you'd access the "dev tools" (usually F12 and/or Ctrl+Shift+I or Cmd+Shift+I on a Mac). Commented May 15, 2016 at 11:09
  • Separately: Since list is irrelevant to the question, it would really be best not to include it in the question. Commented May 15, 2016 at 11:10

1 Answer 1

2

In your search function, you loop through the keys in friends1. The friends1 that search closes over is the one from:

var friends1 = new Object();

...that you never add any properties to. So the loop won't do anything; there are no enumerable properties in the object you're querying.

Throughout search, you want friends, not friends1:

var friends = {
    bill:{
        firstName : "Bill",
        lastName : "Gates",
        number : "(123) 456- 7890", 
        address : ['Microsoft', 20]
        },
    steve:{
        firstName : "Steve",
        lastName : "Jobs",
        number : "(123) 456- 7890",
        address : ['Apple', 30]
        }      
};
var friends1 = new Object();

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

Sign up to request clarification or add additional context in comments.

3 Comments

@ParagBhayani: Looking at it, I'd say it's because it doesn't solve the problem.
for which case specifically?
@ParagBhayani: Actually, it sort of does, but in a very strange way, and with comments descibing things incorrectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.