Reputation: 59
I am writting a dice roller for my website and thanks to my other question I can input data in my function but the Data I ask returns unidentified.
//Constant
var consDie = new Array(3); //Implanté les élément de constance des dés.
for (var i = 0; i <= 2; i++){
consDie[i] = 12 - (i * 2);
console.log("D" + consDie[i]);
}
var consNDice = 6; //Constante pour le nombre de dés
var consAlign = {
UnAl : consDie[2],
Bal : consDie[1],
Phys : consDie[0],
Ment : consDie[0]
};
//declaration of an object that contain the kind of dice that is rolled by powerup
//Variable integers
var intDice = new Array(6); // contenant pour les résultats de dé.
var intPhys = 0; //Phys Aligned answer counter
var intBal = 0; //Neutral/balanced aligned answer counter
var intMent = 0; //Mental aligned answer counter
//Dice Roll function
function Dice (consAlign) {
//Variables constant
this.dicekind = consAlign;
this.rollDice = function() {
return Math.floor(Math.random()*dicekind);
};
}
console.log(Dice(consAlign["Ment"]));
What did I do wrong?
Upvotes: 1
Views: 924
Reputation: 37761
The Dice
function does not return a value. It looks like you meant to use it to construct a new object:
console.log(new Dice(consAlign["Ment"]));
Edit: To follow up with your edit, you are getting NaN
because dicekind
is undefined. 5 * undefined
gives NaN
, "Not a Number".
You would need to use this.dicekind
inside Dice
to make it work. Here's one way to do it work:
function Dice(kind)
{
this._kind = kind;
}
Dice.prototype.roll = function()
{
return Math.floor(Math.random() * this._kind) + 1;
};
var dice = new Dice(6);
console.log(dice.roll());
I've also added 1 in the roll method so that the range of values is from 1..6, instead of 0..5.
Upvotes: 2