Please help! My code does not return who won the game, it just returns "Computer: scissors". although, I do have all the relevant string return. Is there anything you can see that i cant?


#1



var validateInput = function()
{
var userChoice = prompt("Do you choose rock, paper or scissors?")
if(userChoice != "rock" && userChoice != "paper" && userChoice != "scissors")
{
return validateInput()
}
else
{
return userChoice;
}
};

validateInput();

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

};

compare(userChoice,computerChoice);

The code runs fine, but does not return who won the game


Replace this line with your code.


#2

How does one print things out in javascript?

console.log

So to print out the winner, you would need to use that, right?

If you look at your code, have you added any console.log that would print out the winner? Where in the code will control flow be by the time it should be printing out the winner? That's probably where it should go.


#3

Thank you very much for your help! I am still a bit puzzled because the below code does print without even having any console.log but the above does not. I struggled find the place where i can add console.log. Your help would be greatly appreciated.

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

};

compare(userChoice,computerChoice)


#4

Where in the code will program control be when the winner has been determined? Where does the information that you want to print exist at that time?

You may as well just spend some time studying your code if you can't answer those questions, because you need to know in what order your code is executed.

For example if you were to compute the sum of 2 + 3, you might have a program like:

var a = 2;
var b = 3;
var c = a + b;

After having executed those three lines the result is stored in variable c, you can print out the result by calling console.log with c as the argument:

var a = 2;
var b = 3;
var c = a + b;
console.log(c);

You need to be able to do the equivalent reasoning about your rock paper scissors code.


#5

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