Reputation: 11
I am a bit of a noob when it comes to javascript, but I cannot seem to find the reason why this is returning "undefined"
mineOre(userData.pickaxe, playerData.questid);
var newOre = new function () {
var chance = Math.floor(Math.random() * 100);
//console.log(chance);
/*
20% Iron
30% Coal
5% Diamond
3% Emerald
1% Brandons Hair
41% Stone
*/
if (chance < 20) {
return "Iron";
} else if (chance < 50) {
return "Coal";
} else if (chance < 55) {
return "Diamond";
} else if (chance < 58) {
return "Emerald";
} else if (chance < 59) {
return "BrandonsHair";
} else return "Stone";
}
function mineOre(pickaxe, questid) {
console.log(newOre);
}
Upvotes: 0
Views: 7493
Reputation: 414006
You're making the call to mineOre()
before the variable newOre
is initialized. If you move the mineOre()
call to after that initialization, then you'll see that newOre
is an empty object. It's an object and not one of those strings because your code invokes that anonymous function with new
.
If what you want is for newOre
to be one of those strings, get rid of new
and add parentheses after the closing }
of the function:
var newOre = function () {
var chance = Math.floor(Math.random() * 100);
//console.log(chance);
/*
20% Iron
30% Coal
5% Diamond
3% Emerald
1% Brandons Hair
41% Stone
*/
if (chance < 20) {
return "Iron";
} else if (chance < 50) {
return "Coal";
} else if (chance < 55) {
return "Diamond";
} else if (chance < 58) {
return "Emerald";
} else if (chance < 59) {
return "BrandonsHair";
} else return "Stone";
}();
Upvotes: 2