Rock, Paper, Scissors NOT RUNNING


#1



Hello All,

I have completed this project based on Codecademy standards but when I tried to use the code in another complier(JSFiddle), it doesnt run properly. So far, It wil prompt the user but the result is always a tie no matter what the input is. I'm sure I am overlooking something simple but I think with you're help I can figure it out!

:v:

Thank you!

Below is my code:


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);

function compare(userChoice, computerChoice){
    if(userChoice === computerChoice){
        alert("The result is a tie!");
    }
    else if(computerChoice === "scissors"){
        alert("rock wins!");
        if(computerChoice === "paper"){
            alert("paper wins!");
            }
        }
    else if(computerChoice === "rock"){
        alert("rock wins!");
        if(computerChoice === "scissors"){
            alert("scissor wins!");
            }
        }
    else if(computerChoice === "rock"){
        alert("rock wins!");
        if(computerChoice === "paper"){
            alert("scissors wins!");
            }
        }
}
compare();

#2

You call compare, what are you comparing?


#3

I'm calling the compare function that I declared above. This is comparing the user input and the computer choice.


#4

Is it?

Try adding a console.log in your function before the comparison, print out which two values it is comparing.


#5

You've got a condition always giving the same result.

So either the condition is wrong or the values are wrong. So those two are what to look at.


#6

I think i figured it out! I am supposed to call the compare function taking in the two parameters like so:

compare(userChoice, computerChoice);

This works perfectly! :smiley:
Was this what you were suggesting?


#7

Then I would also have to change every instance of computerChoice back to choice1 and change the initial parameters back to choice1 and choice2 .


#8

Why?
Your function isn't (or at least shouldn't) reference anything outside of itself. So why change anything outside of the function in order to affect the function? That's exactly what the function should not be affected by.

Further, choice1 is much less descriptive than computerChoice, that would be a huge downgrade.


#9

Well this is the change I made:

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);

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

alert(compare(userChoice, computerChoice));

Now I the output is always "rock wins!"


#10

Then.. figure out which it is and look at the conditions leading there as well as the values used in the conditions. Same thing as last time.


#11

I changed the code to alert me the computer choice before outputting the results. I'm getting a bit closer but some of the algorithims are still off. I rewrote my code, commenting my understanding of what I think the code should be doing. Could you perhaps let me know if i have written my code in correctly?

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

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

alert(compare(userChoice, computerChoice));

#12

Don't you have to look at both choices to determine who wins? You're looking a whole lot at choice2 but not very much at choice1


#13

I realized that, so I tried this:

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

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

alert(compare(userChoice, computerChoice));

In the instance that I chose rock and the computer chooses paper, the result is undefined.


#14

That suggests that your function didn't return a result. (so the result is undefined since.. you didn't define it)

Keep in mind that's only as accurate as what you're telling me. Didn't read the code.

But since you're only testing for 6 different combinations and there are a total of 9 - yeah you're missing 3.

Note that you're currently testing if choice2 has two different value at the same time - that's not going to happen.


#15

Doesnt the code:

    if(choice1 === choice2){
        return "The result is a tie!";
    }

reconcile for the other 3 possible combinations? I'm assuming those combinations are as follows : rock & rock, paper & paper, and scissors & scissors?


#16

Yeah, that's 3. And then you've got 3 more below.

You've also got some that depend on choice2 having two values at the same time, that is not possible.

The remaining 3 go to the bottom of the function and you'll have specified no return value.


#17

I think i solved it. I separated the second choice2 possibilities outside the brackets of the first possible choice. Seems to be working fine now:

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

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

alert(compare(userChoice, computerChoice));

#18

Now it can test for all possible choices individually instead of at the same time as you mentioned earlier.


#19

A lot of repeated code there though.
The comments are redundant.
Inconsistent indentation.

equal -> tie

rock {
  paper -> paper wins
  scissors -> rock wins
}

paper {
  ..
}

scissors {
  ..
}

#20

How can I consolidate my code to make it more efficient and less redundant?