Difficulty with random property


#1


In my game I want to have the user to be a crewmember of a ship with a randomly determined name so it switches each game. However, when called I simply get "undefined" as the value. I'm sure I'm doing something wrong, but have tried a bunch of different ways and I'm not sure what I'm doing wrong.

Model Code:

user.prototype.randomShipA = function() {
	var randomNum = Math.floor((Math.random()*3)+1);
	if (randomNum === 1) {
		return this.shipa.alpha;
	} else if (randomNum === 2) {
		return this.shipa.beta;
	} else {
		return this.shipa.gamma;
	}
};
user.prototype.randomShipB = function() {
	var randomNum2 = Math.floor((Math.random()*4)+1);
	switch (randomNum2) {
		case 1:
			return this.shipb.delta;
			break;
		case 2:
			return this.shipb.epsilon;
			break;
		case 3:
			return this.shipb.phi;
			break;
		case 4:
			return this.shipb.chi;
	}
};

user.ship = randomShipA() + randomShipB();

Controller call:

var introEnd = alert("Very well " + user.n + ", the " + user.age + " year old crewman of the Rogue Trader vessel " + user.ship + ".");

The user.n and user.age calls are working, but user.ship isn't pulling. Thanks for the guidance.


#2

Here I try to print the value of the attribute name of an empty object:

> console.log({}.name)
undefined

As a result I get undefined, because an empty object has no such attribute.

Perhaps you tried to get a name out of an object that has no such attribute. Perhaps it should have that attribute, or perhaps you should have looked in another object.


#3

@blogrunner69896,

Difficulty with random property

  1. Where is random property?
  2. I can't understand what means this.shipa.alpha? shipa is nested object of user object ?? I think methods should return string value, isn't it?
  3. Does user have property ship?
  4. What is expect result?

I think problem can be in return value or in user's properties. Can you post user object?