Need help with Rock, Paper, Scissors

I just finished writing the code to get a computer choice, and tried to test it. Instead of logging rock, paper, scissors, or even undefined it logs “[Function: getComputerChoice]” with no errors and I have no idea how I did this.

const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (
userInput === “rock” ||
userInput === “paper” ||
userInput === “scissors”
) {
return userInput;
} else {
return “Error, please type rock, paper, or scissors.”;
}
};
const getComputerChoice = () => {
const cpuChoice = Math.random(Math.floor() * 3);
switch (cpuChoice) {
case 0:
return “rock”;
case 1:
return “paper”;
case 2:
return “paper”;
}
};
console.log(getComputerChoice);

That line logs out the object definition, since it is a function, not a primitive value. What we really want to log out is the return value from a call to the function:

let computerChoice = getComputerChoice()

Now we can log it, while keeping that variable in flow.

console.log(computerChoice)
1 Like

Also, you may want to think about this statement.

const cpuChoice = Math.random(Math.floor() * 3);

Try printing out cpuChoice.
You are expecting an integer 0 or 1 or 2.
Do you see what you expect? If not, why so?

2 Likes

You get an error and that would be because cpuChoice isn’t defined outside of the function getComputerChoice.

What is condsidered a primitive value?

I tried this and it came back undefined

const getComputerChoice = () => {
cpuChoice = Math.random(Math.floor() * 3);
switch (cpuChoice) {
case 0:
return “rock”;
case 1:
return “paper”;
case 2:
return “paper”;
}
};
let computerChoice = getComputerChoice()
console.log(computerChoice);

Math.random() does not take any seed argument, as far as I am aware. It’s chief role is to generator a float between 0, inclusive and 1, exclusive (1 is not a float).

let x = Math.random()
console.log(0 <= x && x < 1)    //  true

Math.floor() does need an argument, a float (or number) that is truncated to preserve only the leading integer.

console.log(Math.floor(Math.PI))    //  3

Given the above argument restrictions for random and floor, we can only conclude that the expression to return a random integer between 0 and n-1, inclusive, is,

let r = Math.floor(Math.random() * n)

Above, r will be one of, 0, 1 or 2 when n is 3.


Aside

1 is a float, in JavaScript. There are no other number types, but Number which includes the set of Real Numbers, including Infinity and -Infinity. If we want integers we have to use methods to parse them out, such as the method above. Just remember, Math.random() is always a decimal fraction that can equal zero, but cannot equal 1.

2 Likes

Math.random() can never equal 1 which is why Math.random(Math.floor) * 3) will put out a 3. While you have given me greater insight into the Math object I still don’t understand what it is about my code that makes the console log undefined.

1 Like
 > Math.random(Math.floor) * 3)
 X Uncaught SyntaxError: Unexpected token ')'
 > 

Thats just a rookie mistake I made in my reiteration of what you told me. Why does this return ‘undefined’ and not one of the strings that are specified?

const x = function getComputerChoice() {
const cpuChoice = Math.random(Math.floor() * 3);
switch (cpuChoice) {
case 0:
return “rock”;
case 1:
return “paper”;
case 2:
return “scissors”;
}
};

console.log(x())

1 Like

None of the cases have been satisfied so JS is returning the default, undefined. We can substitute our own value for those cases that do not meet the switch requirements.

default: return "Oops!"

Add that to your switch in the last position and run the code. What happens?

It returned Oops just like you said. Which means it isn’t getting 0, 1, or 2. Which takes us back to the Math.random(Math.floor() * 3). Meaning I must have input it wrong somehow. Or it isn’t attaching the variable to the switch.

1 Like

Excellent conclusion. Go back up a couple posts and review the examples. That should help you solve this error.

I feel like a moron. All I had to do was flip .random and .floor for it to work. I was going over what you told me and also referring to the Race day project I did where I played with this

let raceNumber = Math.floor(Math.random() * 1000);

const time = true

const earlyAdult = time

let runnerAge = Math.floor(Math.random() * 100);

and I finally saw it and remembered what you said about .random not needing an argument and it all made sense.

Thank you for your time and patience.