Build a fortune teller

I’m struggling with the tests 7 and 9:

7. You should generate a random number between 1 and 5, inclusive, and assign it to the variable randomNumber.

9. The randomNumber should correspond to its fortune. For example, if randomNumber is 1, the selectedFortune should be equal to fortune1 and so on.

The code is working for all 5 numbers, there are no error messages when i run it - it just logs the corresponding string to the console.

For test 7, i have tried it with hard coded values and with variables, both don’t pass.

What am I doing wrong? Thanks for your help

```

const fortune1 = "The Lovers"
const fortune2 = "The Hermit"
const fortune3 = "The World"
const fortune4 = "The Chariot"
const fortune5 = "The Empress"
const max = 5
const min = 1
let randomNumber = Math.round(Math.random() * (max - min) + min)

let selectedFortune
if (randomNumber == 1) {selectedFortune = fortune1}

else if (randomNumber == 2) {selectedFortune = fortune2}

else if (randomNumber == 3) {selectedFortune = fortune3}

else if (randomNumber == 4){selectedFortune = fortune4}

else if (randomNumber == 5) {selectedFortune = fortune5}


console.log(selectedFortune)

```

Welcome to the forum @cammaratasandra736

Please post a link to the lab.

Happy coding

Is Math.round() the best choice here?

Math.random() - JavaScript | MDN

Changed it to Math.ceil, and it passed. Thank you so much!

I’m just curious: why is Math.ceil the better choice here? Math.floor obviously doesn’t work, because it doesn’t include 5, but Math.round does.

Is it because with Math.round, the number 5 is less likely to appear compared to the other numbers, and with Math.ceil, their probabilities are equal?

It really should be Math.floor(), but remember that max is not inclusive, so your max value should be 6 in this case.

Scroll up a bit in that link I sent you and you’ll see this:

The returned value is no lower than (and may possibly equal) min, and is less than (and not equal)max .

1 Like

Thank you, that helped tremendously!