What did I do wrong? (act 2)

js

#1

So, I got halfway through the Javascript course and I decided to build something relatively simple with my current knowledge.

This is what I made:

// casino machine thingy
let yourWallet = 50000;
let yourBet = 10000;

const youFailed = yourBet * 0;
const youLost = yourBet * 0.5;
const youAreSafe = yourBet * 1;
const youGotSmallPrize = yourBet * 1.25;
const youGotMediumPrize = yourBet * 1.5;
const youGotDoubled = yourBet * 2;
const youGotBigPrize = yourBet * 5;
const youGotLegendaryPrize = yourBet * 25;
const youWonTheWholeCasino = yourBet * 666;

const theRoll = Math.floor(Math.random * 1000);

if (0 <= theRoll && theRoll <= 200) {
  yourWallet = yourWallet - yourBet + youFailed;
  console.log("Your balance is now " + yourWallet);
} else if (200 < theRoll && theRoll <= 400) {
  yourWallet = yourWallet - yourBet  + youLost;
  console.log("Your balance is now " + yourWallet);
} else if (400 < theRoll && theRoll <= 600) {
  yourWallet = yourWallet - yourBet  + youAreSafe;
  console.log("Your balance is now " + yourWallet);
} else if (600 < theRoll && theRoll <= 700) {
  yourWallet = yourWallet - yourBet  + youGotSmallPrize;
  console.log("Your balance is now " + yourWallet);
} else if (700 < theRoll && theRoll <= 800) {
  yourWallet = yourWallet - yourBet  + youGotMediumPrize;
  console.log("Your balance is now " + yourWallet);
} else if (800 < theRoll && theRoll <= 900) {
  yourWallet = yourWallet - yourBet  + youGotDoubled;
  console.log("Your balance is now " + yourWallet);
} else if (900 < theRoll && theRoll <= 975) {
  yourWallet = yourWallet - yourBet  + youGotBigPrize;
  console.log("Your balance is now " + yourWallet);
} else if (975 < theRoll && theRoll <= 999) {
  yourWallet = yourWallet - yourBet  + youGotLegendaryPrize;
  console.log("Your balance is now " + yourWallet);
} else if (theRoll === 1000) {
  yourWallet = yourWallet - yourBet  + youWonTheWholeCasino;
  console.log("Your balance is now " + yourWallet);
};

Something is definitely wrong with it, because I expect a number and instead I get “undefined”. What exactly did I do wrong?


#2

i slightly modified your code:

// casino machine thingy
let yourWallet = 50000;
let yourBet = 10000;

const youFailed = yourBet * 0;
const youLost = yourBet * 0.5;
const youAreSafe = yourBet * 1;
const youGotSmallPrize = yourBet * 1.25;
const youGotMediumPrize = yourBet * 1.5;
const youGotDoubled = yourBet * 2;
const youGotBigPrize = yourBet * 5;
const youGotLegendaryPrize = yourBet * 25;
const youWonTheWholeCasino = yourBet * 666;

const theRoll = Math.floor(Math.random * 1000);

console.log(theRoll)

if (0 <= theRoll && theRoll <= 200) {
  yourWallet = yourWallet - yourBet + youFailed;
  console.log("Your balance is now " + yourWallet);
} else if (200 < theRoll && theRoll <= 400) {
  yourWallet = yourWallet - yourBet  + youLost;
  console.log("Your balance is now " + yourWallet);
} else if (400 < theRoll && theRoll <= 600) {
  yourWallet = yourWallet - yourBet  + youAreSafe;
  console.log("Your balance is now " + yourWallet);
} else if (600 < theRoll && theRoll <= 700) {
  yourWallet = yourWallet - yourBet  + youGotSmallPrize;
  console.log("Your balance is now " + yourWallet);
} else if (700 < theRoll && theRoll <= 800) {
  yourWallet = yourWallet - yourBet  + youGotMediumPrize;
  console.log("Your balance is now " + yourWallet);
} else if (800 < theRoll && theRoll <= 900) {
  yourWallet = yourWallet - yourBet  + youGotDoubled;
  console.log("Your balance is now " + yourWallet);
} else if (900 < theRoll && theRoll <= 975) {
  yourWallet = yourWallet - yourBet  + youGotBigPrize;
  console.log("Your balance is now " + yourWallet);
} else if (975 < theRoll && theRoll <= 999) {
  yourWallet = yourWallet - yourBet  + youGotLegendaryPrize;
  console.log("Your balance is now " + yourWallet);
} else if (theRoll === 1000) {
  yourWallet = yourWallet - yourBet  + youWonTheWholeCasino;
  console.log("Your balance is now " + yourWallet);
} else {
  console.log("else clause")
}

i added an else clause, it shouldn’t reach there, but it did. So something is wrong with the comparison.

So i added a console.log to see the value of theRoll (given that is used in the comparison a lot), to see its value, it show NaN (not a number), that can’t be right. Something is wrong with your random number generator

which explains why the else clause is being triggered

now, i haven’t fixed the issue yet, i will leave that to you, but i wanted to show how i debugged your code quickly


#3

I see. Thanks for the reply


#4

I experimented with Math.random a little in the browser console and now I am pretty confused.
I thought that Math.random was an infinitely precise number between 0(inclusive) and 1(exclusive), so if I do console.log(Math.round(Math.random * 10)); I get a number between 0(inclusive) and 10(inclusive), but instead I get “undefined”.
Adding to the injury, if I simply do console.log(Math.random); I still get “undefined”.

Math.random is a lie?!


#5

i thought it was also exclusive zero, you can have something like 0.000000001 but i thought 0 itself was also not possible. But you can check documentation for this

You have successfully isolated the problem. Maybe look at the documentation for math random (google: MDN math random) to see what you did wrong?

MDN = mozzila developers network, a good place for JS documentation


#6

I researched and they all say the same thing as I image
image
image


#7

Careful with the use of w3schools.

Nope? I clearly see a set of parentheses in the documentation involving random() which i do not see in your code


#8

Ohhhh I see now, I need to add a set of parentheses for it to work. Man, I feel dumb now. Thanks for the help.


#9

yes, random is a method (or a function, i think a method, too lazy to check), calling a method (and function) requires parentheses.