Unable to get info using prompt instead of passing value through function


#1



It's the link to the Contact list exercise of Javascript -- https://www.codecademy.com/en/courses/javascript-beginner-en-3bmfN/0/8?curriculum_id=506324b3a7dffd00020bf661#


After making the contact list, I decided to make some additional changes. All I did was to add a prompt to ask user to enter the friend's name and the category and then used it in the function. But when I enter Steve as the first prompt's answer and friends in the second prompt's answer, the output does not return anything. Here's my code :


var friends = {
    steve : steve,
    bill : bill
    }
var steve = {
    firstName : "Steve",
    lastName : "Morata",
    number : "8055805529",
    address : ["One Microsoft Way", "Redmond", "WA", "98052"]
    };
var bill = {
    firstName : "Bill",
    lastName : "Saunders",
    number : "8044804429",
    address : ["Zoo", "Behind tree no. 43", "Cage no. 2"]
    };
var list = function(name){
    for(var props in name){
        console.log(name[props].firstName);
        }
    console.log("That's it!")
    }
list(friends);
var search = function(){
    var fName = prompt("Whom do you want to search for?","Type Name");
    var category = prompt("Which category does that contact belong to?","Type Category");
    for (var names in category){
        if (category[names].firstName === fName){
            console.log(category[names])
            }
        }
    }
    search();

But if I change the search function to the following it works:

var search = function(category){
    var fName = prompt("Whom do you want to search for?","Type Name");
    for (var names in category){
        if (category[names].firstName === fName){
            console.log(category[names])
            }
        }
    }
    search(friends);

I don't understand what's the problem in passing the category name friends through prompt. I made that thinking it would be easier if we had more contacts of different categories.


#2

@datamaster40343,

-1
First of all your Data is a bit off.....
With

var friends = {
    steve : steve,
    bill : bill
    };

you are expecting the variables steve and bill to be already defined !!!!!
So you will have to change the =order= of your object-definition....
like

var steve = {
    firstName : "Steve",
    lastName : "Morata",
    number : "8055805529",
    address : ["One Microsoft Way", "Redmond", "WA", "98052"]
    };

var bill = {
    firstName : "Bill",
    lastName : "Saunders",
    number : "8044804429",
    address : ["Zoo", "Behind tree no. 43", "Cage no. 2"]
    };
var friends = {
    steve : steve,
    bill : bill
    };

-2
You want to access the variable name's which are representing an object
and you do that by using the prompt()-Method.
The prompt()-Method will however return a string Value.....

Javascript gives you 2 possibilities

  • // using evel eval
    var category = eval(prompt("xx","friends"));
    console.log(category);
  • // accessing the =global= variable via =windows= Object
    var category = window[prompt("xx","friends")];
    console.log(category);

#3

Thanks! :smile: The eval function works but could you please explain it in detail. What the function does?

// using evel eval
var category = eval(prompt("xx","friends"));
console.log(category);

But the window method does not work :sweat:

// accessing the =global= variable via =windows= Object
var category = window[prompt("xx","friends")];
console.log(category);


#4

@datamaster40343,
Present the code, in which you integrated the =solutions=....
and this context you can only give a prompt() answer
steve
bill
friends

It would also be interresting to know which Browser+Versionnr on which Operating-System
you are using.....

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types


#5

Actually :sweat_smile: I didn't notice the square brackets in window function before. When I corrected my code, it worked too. :smile: Thanks a lot!
Here's my code :

var steve = {
    firstName : "Steve",
    lastName : "Morata",
    number : "8055805529",
    address : ["One Microsoft Way", "Redmond", "WA", "98052"]
    };
var bill = {
    firstName : "Bill",
    lastName : "Saunders",
    number : "8044804429",
    address : ["Zoo", "Behind tree no. 43", "Cage no. 2"]
    };
var friends = {
    steve : steve,
    bill : bill
    }
var list = function(name){
    for(var props in name){
        console.log(name[props].firstName);
        }
    console.log("That's it!")
    }
list(friends);
var search = function(){
    var fName = prompt("Whom do you want to search for?","Type Name");
    var category = window[prompt("Which category does that contact belong to?","Type Category")];
    //var category = eval(prompt("Which category does that contact belong to?","Type Category"));
console.log(category);
    for (var names in category){
        if (category[names].firstName === fName){
            console.log("Here's the info you asked for : ")
            console.log(category[names])
            }
        }
    }
    search();