0

I apologize if my title is confusing in any way. I'm trying to figure out how to properly do this. I'm trying to create an object whos key:values are other objects. See the code below. I'm testing in the Chrome console.

If I just do Characters = CharactersFn("male"); or var Characters = CharactersFn("male"); by itself I can create the object from the CharactersFn() function but when I try to do it via my whatAreYou() function I get no results. How do I do this properly?

Note: I'm still learning and just trying to get a grasp on how to do things properly.

var Characters,
    valueArr = [],      
    nameArr = [],           
    matchArr = [];

var CharactersFn = function (ans) {     //Are you male or female?   
    "use strict";
    if (ans === "male") {
        Characters = {
            47: aloy,
            snake: snake,
            drake: drake,
            cloud: cloud
        };
    }

    if (ans === "female") {
        Characters = {
            aloy: aloy,
            bayonetta: bayonetta,
            elizabeth: elizabeth,
            ellie: ellie
        };
    }
    return Characters;
};

function whatAreYou() {
    "use strict";
    var gender = prompt("0 or 1");

    if (gender === 0) {
        Characters = CharactersFn("female");
    }
    if (gender === 1) {
        Characters = CharactersFn("male");
    }
        return Characters;
}
2
  • "use strict"; should be at the top of a JS file, not inside a function. stackoverflow.com/questions/1335851/… Commented Apr 26, 2017 at 16:19
  • thanks for the note! Commented Apr 26, 2017 at 17:35

2 Answers 2

4
var gender = prompt("0 or 1");
if (gender === 0) {
if (gender === 1) {

The prompt function returns a string. The result will never match either of your if statements.

You need to compare to "0" and "1" not 0 and 1.

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

3 Comments

Also, prompt could return null, which isn’t covered in that code.
@Xufox I'll definately check for that. I just tried to trim my example down as much as possible to show what I was trying to do
@Quentin That looks like it! It's always one of those simple things. I should have known better.
0

You are setting the global Characters object equal to an object in your CharactersFn when you call it. You are also then returning the global Characters object from CharactersFn. Then in your whatAreYou function, you are setting the global Characters object to the result of the CharactersFn (which like I said you have returning the global Characters object). You were also creating objects with values that references variables that were not defined so I changed them to string values. Also, as mentioned above you were using an equality comparison (===) on string and int values. If you used the == then it would work but with the === the values need to be identical.

Here is an updated version that will set the global Characters object to the result of the CharactersFn which I have modified to return the objects them selves.

var Characters = {},
    valueArr = [],      
    nameArr = [],           
    matchArr = [];

var CharactersFn = function(ans) {   
    if (ans === "male") {
        return  {
            47: "aloy",
            snake: "snake",
            drake: "drake",
            cloud: "cloud"
        };
    }

    if (ans === "female") {
        return {
            aloy: "aloy",
            bayonetta: "bayonetta",
            elizabeth: "elizabeth",
            ellie: "ellie"
        };
    }
    return Characters;
};

function whatAreYou() {
    var gender = prompt("0 or 1");

    if (gender === "0") {
        Characters = CharactersFn("female");
    }
    if (gender === "1") {
        Characters = CharactersFn("male");
    }
        return Characters;
}

console.log(Characters);
whatAreYou();
console.log(Characters);

1 Comment

I'll keep all that in mind. The only thing here I won't change is where you change is where you say I'm "creating objects with values that references variables that were not defined". I suppose I wasn't clear enough in my description but these are actually other objects. They are not defined in my example because I wanted to keep the example as simple as possible.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.