#do these 2 code blocks do the same thing

3 messages · Page 1 of 1 (latest)

broken jewel
#

Question more out of curiosity. In module 3, blackjack game, working in the get RandomCard function. I wrote

    let randomNumber = Math.floor( Math.random()*13 ) + 1;
    if (randomNumber === 1) {
        randomNumber === 11;
    } else if (randomNumber >= 11) {
        randomNumber = 10;
    } else {
        randomNumber;
    }
    return randomNumber;
}```

and Per wrote
```function getRandomCard() {
 Math.floor gives a whole number
    let randomNumber = Math.floor( Math.random()*13 ) + 1;
    if (randomNumber === 1) {
        return 11;
    } else if (randomNumber > 10) {
        return 10;
    } else {
        return randomNumber;
    }
}```

Does my code still do the same as Per's, just written slightly differenlty?
inner steppe
#

The result would be the same.
The only difference, that you reuse randomNumber variable and re-assign its value, then you return it in the end.

The other code block uses early return statements without re-assigning randomNumber variable.

I would personally prefer to write it this way, but all of these essentially get you the same result.

const getRandomCard = () => {
    const randomNumber = Math.floor(Math.random() * 13) + 1;

    if (randomNumber === 1) return 11;
    if (randomNumber > 10) return 10;
    return randomNumber;
}
broken jewel
#

Thanks @inner steppe for the explanation, really helpful