Rock, Paper, Scissors, is always Rock?!


#1


https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661


Hi guys, my code gets me through the course, but in reality it isn't returning anything. Basically the computerChoice is always Rock.

Please help.


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
    if(choice1 === "Paper") {
        if(choice2 === "Rock") {
            return "Paper wins";
        }else {
            return "Scissors wins";
        }
    }
    else
    if(choice1 === "Scissors") {
        if(choice2 === "Paper") {
            return "Scissors wins";
        }else {
            return "Rock wins";
        }
    }
    };


#2

shouldn't it prompt the user or log the outcome? It isn't returning the outcome... why?


#3

string comparison is case sensitive, so lets say the user picks rock, this:

if ("rock" == "Rock")

is false, the strings don't match. same for computerChoice


#4

Thanks! but I still don't get a prompt saying I've won or lost or tied... should I be using the "prompt" comand somewhere?


#5

can i see an updated version of your code?


#6

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
    if(choice1 === "paper") {
        if(choice2 === "rock") {
            return "paper wins";
        }else {
            return "scissors wins";
        }
    }
    else
    if(choice1 === "scissors") {
        if(choice2 === "paper") {
            return "scissors wins";
        }else {
            return "rock wins";
        }
    }
    };

Here it is, I tried to correct all the "case sensitive" issues, but still doesn't work as it should


#7

compare is a function, a function only execute when called. its step 3 of exercise 8.

if you don't call the function, it won't execute, so a winner is never determined


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.