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;
}
AGx-07_162
  • 301
  • 1
  • 3
  • 14

2 Answers2

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
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);
Ken
  • 466
  • 2
  • 7
  • 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. – AGx-07_162 Apr 26 '17 at 17:42