Need help with this program


#1

var compChoice;
var choice;
var compare = function(choice, compChoice)
{
    if(choice = compChoice) //this is a tie
    {
        console.log("Sorry no one won it was a tie!!");
    };
    if(Choice == "rock" && compChoice == "scissors")
    {
        console.log("You win!!");
    };
    if(choice == "rock" && compChoice == "paper")
    {
        console.log("You lose!!");
    };
    if(choice == "paper" && compChoice == "rock")
    {
        console.log("You win!!");
    };
    if(choice == "paper" && compChoice == "scissors")
    {
        console.log("You lose!!");
    };
    if(choice == "scissors" && compChoice == "rock")
    {
        console.log("You lose!!");
    };
    if(choice == "scissors" && compChoice == "paper")
    {
        console.log("You win!!");
    };
};
compChoice = math.ceil(math.random() + math.random() + math.random() + 0.10);
choice = prompt("Ready? Select rock, paper, or scissors"); 
if(compChoice > 0 && compChoice <= 3)
{
    if(compChoice = 1)
    {
        compChoice = "rock";
    };
    if(compChoice = 2)
    {
        compChoice = "paper";
    };
    if(compChoice = 3)
    {
        compChoice = "scissors";
    }; 
};
compare(choice, compChoice);

#2

Ok first of all avoid semicolons after conditional statements:


If you do so you can even use else if without getting an error.

Also here you use a = which is an assignment operator instaed of the comparison operators == or ===:

if(choice = compChoice) //this is a tie

and here you used choice with an upper case "C":

if(Choice == "rock" && compChoice == "scissors")

last but not least your compChoice ways of choosing a number seems to be pretty unfair:

compChoice = math.ceil(math.random() + math.random() + math.random() + 0.10);

Also 4 is possible which is not covered in your cases.

PS: If you want to stick to your code then you might need to use return instead of console.log as the test function expects at least a returned value.


#3

Here is another way to solve 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";
} 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";   
    }
}

};
compare(userChoice, computerChoice);
============================================================
I m not saying that s the only way, your approach could work too but it s not the easiest/shortest solution.

But if you want to stick with your code, you need to
* pay attention to the typo (Choice !== choice),
* try to use else if statement instead of creating new if each time, cs the computer will check each of them instead of stopping when it hit the right/correct condition - (true) -,
* when you re trying to check for two condition in an if you need to separate them
eg. if ((compChoice === "rock") && (compChoice === "scissors"))
* = is for assign
== is for compare
=== stands for "strictly equal to"
here you want to use ===


#4

Thanks!! But now i'm getting a "math not defined error"..what does that mean?


#5

You probably need to write Math with an upper case "M" sry just copy and pasted it from your code and missed it.


#6

Hey thanks!! you were right!! Thanks for the help Sir or Madame (I'm assuming sir, though)!!


#7

Sir but that sounds really formal :smile: