Please help me with Computer Choice: Part 2


#1

Hello,

I think there is something that I am missing here. I keep getting various syntax errors, mainly "Expected ;". Here is my code:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random() {
if (computerChoice <= .33) {
return "rock"
}
else if (computerChoice <= .66) {
return "paper"
}
else {
return "scissors"
}}
console.log(computerChoice);


#2

Hey @zneuhaus,

The problem is here :smiley:

Though Math.random is a function, it's a function that takes out a random math number. But it's not really a function. All functions that are made have two things in common:

  • They have parameters
  • They both include the word "function" in it

About the common things, this is why:

var blahblee = function(parameter){}
function blahblee(parameter){}

Both ways work, see?:smiley:

But it's different for Math.random. Math.random is just a simple Math.random();

Hope you get it!:smiley:


#3

You can't use return because you are not using it inside a function.

Here is another way you might write it...

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();

if (computerChoice < 0.34) {
computerChoice = "rock";
}
else if (computerChoice <= 0.67) {
computerChoice = "paper";
}
else {
computerChoice = "scissors";
}


#4

Math.random() actually is a function or better a method of the Math object the point is it was written for you so you don't need to write it yourself but you can just use it by calling it. Also parameters can be optional.


#5

Thank you all for the quick replies and help! :smile: