Computer Choice computerChoice = Math.random();


#1

Why won't this work?

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log("Computer Choice: " + computerChoice);
var userChoice = userChoice
if (computerChoice < 0.34) {
	computerChoice = "rock";
} 
else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} 
else {
	computerChoice = "scissors";
} 
console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        return "The result is a tie!";
    }
    else if(choice1 === "rock") {
        if(choice2 === "scissors") {
            return "rock wins";   
        }
        else {
            return "paper wins";
        }
    }
    else if(choice1 === "paper") {
        if(choice2 === "rock") {
            return "paper wins";
        }
        else {
            return "scissors win";
        }
    }
    else if(choice1 === "scissors") {
        if(choice2 === "rock") {
            return "rock wins";
        }
        else {
            return "scissors win";
        }
    }
}
compare(userChoice, computerChoice)

#2

What is the error message?


#3

Did you set var computerChoice = Math.random() ? The capital 'M' in 'Math' is important!


#4

Refresh the page and try again? What exercise does this code belong to?


#5

Build Rock, Paper Scissors

is the exercise under the Javascript course

I have refreshed, reset and everything. The console output is good. I just get thrown this error which as far as I can tell is wrong.


#6

i copied your code and run it and and it works fine if you remove console.log after the
var ComputerChoice = Math.random();

console.log("Computer Choice" + computerChoice);


#7

I did that

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var userChoice = userChoice;
if (computerChoice < 0.34) {
	computerChoice = "rock";
} 
else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} 
else {
	computerChoice = "scissors";
} 
var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        return "The result is a tie!";
    }
    else if(choice1 === "rock") {
        if(choice2 === "scissors") {
            return "rock wins";   
        }
        else {
            return "paper wins";
        }
    }
    else if(choice1 === "paper") {
        if(choice2 === "rock") {
            return "paper wins";
        }
        else {
            return "scissors win";
        }
    }
    else if(choice1 === "scissors") {
        if(choice2 === "rock") {
            return "rock wins";
        }
        else {
            return "scissors win";
        }
    }
}
compare(userChoice, computerChoice)

Still same error


#8

var rock = "rock";
var scissors = "scissors";
var paper = "paper";
var randomArray = [rock, scissors, paper];


var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        return -1;
    }
    else if(choice1 === rock) {
        if(choice2 === scissors) {
            return choice1;
        }
        else {
            return choice2;
        }
    }
    else if(choice1 === paper) {
        if(choice2 === rock) {
            return choice1;
        }
        else {
            return choice2;
        }
    }
    else if(choice1 === scissors) {
        if(choice2 === rock) {
            return choice1;
        }
        else {
            return choice2;
        }
    }
};


var play = function(){
    var computerChoice = randomArray[Math.floor(Math.random()*randomArray.length)];
    var userChoice = prompt("Do you choose rock, paper or scissors?");
    if (userChoice){
        userChoice = userChoice.trim().toLowerCase();
        if(randomArray.indexOf(userChoice) == -1){
            alert("Wrong input, try again.");
            play();
        }else{
            console.log("Your choice: " + userChoice);
            console.log("Computer Choice: " + computerChoice);
            var result = compare(userChoice, computerChoice);
            if (result != -1){
                console.log(result + " wins.");
            }else{
                console.log("its a tie.");
            }
            play();
        }
    }
};
play();

This doesn't work either.

I am starting to get pissed off because this is valid JS


#9

try this one

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";
} console.log("Computer: " + computerChoice);

var compare = function(choice1,choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
} else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    } else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    } else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return ("rock wins") ;
        } else {
            return ("scissors wins") ;
        }
    }
};

compare(userChoice, computerChoice);

#10

ok I will give it a shot. standby for frustration lol


#11

Oops, try again. Did you set var computerChoice = Math.random() ? The capital 'M' in 'Math' is important!

I really don't understand. This has to be a bug.


#12

which browser do you use? try other one..


#13

I am using chrome. I will try safari


#14

No luck in Safari either. Same error message about Math.random()


#15

I meant what exercise of this particular lesson you're using this code on. As I only know this error from 3/9 when you already have a string output but the exercise just expects computerChoice to be a number.


#16

This is the code for this exersice; Solution
1.var validate_input = function() {
2.userChoice = prompt("make your choice: ");
3./* validate the user choice, if valid, return userChoice */
4.if (userChoice === "rock") {
5.return userChoice;
6.} else if (userChoice === "paper") {
7.return userChoice;
8.} else if (userChoice === "scissors") {
9.return userChoice;
10.} else {
11./* invalid choice, the function need to run again */
12.return validate_input();
13.}
14.}
15.
16.validate_input();
17.var userChoice = prompt("rock", "paper", "scissord");
18.var computerChoice = Math.random(userChoice);
19.console.log(computerChoice);