1

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);
    }
  • 2
    Every function returns `undefined` by default if no `return` statement is provided. In your case `mineOre` definitely returns `undefined` because it doesn't return anything. And since `newOre` is a function, probably what you were trying to do there is `console.log(newOre());` – choz Oct 06 '18 at 13:36
  • 1
    @choz however (though it's hard to tell exactly what's going on) the anonymous function is invoked with `new`, so call should set `newOre` to an empty object (the string return values would be ignored). – Pointy Oct 06 '18 at 13:39
  • 1
    Ah but the call to `mineOre()` is made before `newOre` is initialized, so it's `undefined` at that point. Now it makes sense :) – Pointy Oct 06 '18 at 13:42
  • 1
    @Pointy and @undefined - You guys are right. I totally didn't pay attention to that `new` keyword. – choz Oct 06 '18 at 13:42
  • [Never ever use `new function`](https://stackoverflow.com/questions/10406552/is-it-right-to-think-of-a-javascript-function-expression-that-uses-the-new-key-as-static) - and it doesn't even work in your case – Bergi Oct 06 '18 at 13:49

1 Answers1

2

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";
}();
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Well I copied his code directly into a file to run with Node and it logged `undefined`. Moving the function call to after the declaration made it log `{}`. – Pointy Oct 06 '18 at 13:50