0

I'm learning JavaScript and have this very simple app that returns a random item from an array, but only if it's different to the previous item returned. My code is below. It works fine generally, but randomly returns undefined sometimes even when the console.log on the previous line returns a valid item. If I have the return outside of the If statement it never returns undefined.

Why does my function only sometimes return undefined when inside the If statement and work fine outside?

let ranArray = [
    "flaky","pickle","grass","juicy","desert","wonderful","even","signal","frail","team","addition"
    ];

let previousRanNum = 12;

function generateNumber (){
    return Math.floor(Math.random()*11);
};

function getItem(){

    let ranNum = generateNumber();

    if (ranNum !== previousRanNum) {
        previousRanNum = ranNum;
        console.log(ranNum);
        console.log(ranArray[ranNum]);
        return ranArray[ranNum];
    } else {
        getItem();
    }
};

debug console:

getItem()
undefined
10
addition

The following works fine and never return undefined:

function getItem(){

    let ranNum = generateNumber();

    if (ranNum !== previousRanNum) {
        previousRanNum = ranNum;
        console.log(ranNum);
        console.log(ranArray[ranNum]);
    } else {
        getItem();
    }
    return ranArray[ranNum];
};
Paul
  • 67
  • 2
  • 10
  • 4
    In the `else` branch, where you recursively call `getItem()`, you're missing a `return`. – Pointy Jan 23 '18 at 18:54
  • Don't use recursion for things like this, use a loop. – Barmar Jan 23 '18 at 19:01
  • Thanks Pointy. Why would that matter because when the if test is truthy the else shouldn't be run, should it? I've just tested with a return after getItem() and still, eventually, got undefined. – Paul Jan 23 '18 at 19:01
  • Thanks for advice Barmer. Still curious about this though... – Paul Jan 23 '18 at 19:02
  • 1
    You want the `return` to be *before* the recursive call, not after. `return getItem()` This way it actually returns the value produced by the recursive call. –  Jan 23 '18 at 19:06
  • 1
    https://jsfiddle.net/jzhhkr3L/2/ –  Jan 23 '18 at 19:12
  • Thanks rock star that's fixed it. I'm still curious about my original question though.. what causes it to randomly return undefined? And why does the return in the else block affect the return when the If is true? – Paul Jan 23 '18 at 19:18
  • 1
    It randomly returned `undefined` because it randomly went into the `else` where nothing was being returned. Now it returns the result of the recursive call. –  Jan 23 '18 at 22:39
  • Rock star - I can see the update you made to your jsfiddle and that's helped me to understand it, thanks. I'm guessing as there was no return in the else block, when the function is called again the return is somehow retained as `undefined` from the previous call? Because I can see the true condition is being run because of the console.log and the return in the true condition should be valid, yet `undefined` is returned. Anyway, loads yet to learn so thanks for your time. – Paul Jan 23 '18 at 23:08

0 Answers0