0
var gameFunction = function()
{
    var userChoice = prompt("What do you choose: rock, paper,   or scissors?")

    var computerChoice = Math.random();

    if (0 < computerChoice < 0.33)
    {
        computerChoice = "rock";
    }
    else if (0.33 < computerChoice < 0.67)
    {
        computerChoice = "scissors";
    }
    else
    {
        computerChoice = "paper";
    }

    console.log("Computer choice: ",computerChoice)

    if (userChoice === computerChoice)
    {
        return "The result is a tie! Enter a new result?"
        gameFunction();
    }

    else if (userChoice === "rock")
    {
        if (computerChoice === "scissors")
        {
            return "rock wins"
        }
        else if (computerChoice === "paper")
        {
        return "paper wins"
        }
    }

    else if (userChoice === "paper")
    {
        if (computerChoice === "rock")
        {
            return "paper wins"
        }
        else if (computerChoice === "scissors")
        {
            return "scissors win"
        }
    }

    else if (userChoice === "scissors")
    {
        if (computerChoice === "paper")
        {
            return "scissors wins"
        }
        else if (computerChoice === "rock")
        {
            return "rock win"
        }
    }
}

gameFunction();

This is the 9/9 section of the "Rock paper scissors" game from Codecademy: Javascript.

My problem is this:

When the User and Computer ties, it's supposed to re-run the entire "gameFunction" function, meaning it should ask a new input from the user and get a new input from the computer.

However, the program just prints out "The result is a tie!" without re-running "gameFunction." How can I fix this?

2
  • if (0 < computerChoice < 0.33) ==> if (0 < computerChoice && computerChoice < 0.33) Commented Jun 30, 2016 at 7:20
  • 1
    After return, no more code gets executed inside the function Commented Jun 30, 2016 at 7:21

6 Answers 6

1

No line executed after return statement.. try

 gameFunction();
 return "The result is a tie! Enter a new result?"
Sign up to request clarification or add additional context in comments.

2 Comments

But then it will eventually display the result of the original tied game after the second game is finished. Wouldn't that confuse the player?
u can give a delay by setTimeout function. depend upon your logic and requirements
1

The return statement exits the "gameFunction" function so it doesn't execute the next line. Try using a prompt instead like this:

if (userChoice === computerChoice)
{
    prompt("The result is a tie! Enter a new result?");
    gameFunction();
}

This way the user can respond to your prompt and you can use it to decide if the game is to continue. You could always just use an alert as well :)

Comments

0

Change return to alert(), like below:

From:

return "The result is a tie! Enter a new result?"

To:

alert("The result is a tie! Enter a new result?");

Comments

0

what about this: https://jsfiddle.net/41gcfL6g/

The thing here is adding a parameter to the function, so you can determine if have been a tie the last time you played. And then in the case of tie, instead of calling the function after the return, you return the result of the gameFunction

Comments

0

The recusive gameFunction() method inside the gameFunction() because the control returns to the first calling function.

if (userChoice === computerChoice)
{
    return "The result is a tie! Enter a new result?"
    gameFunction();
}

So instead of return you can print a message there showing there is a tie.

if (userChoice === computerChoice)
{
    alert("The result is a tie! Enter a new result?")
    gameFunction();
}

and when the above condition is not met then it simply returns to the calling area and stop.

Comments

0

see fiddle, don't use return, console.log https://jsfiddle.net/o62vda05/

var userChoice;

function startGame()
{
    userChoice = prompt("What do you choose: rock, paper,   or scissors?");
    gameFunction(); 
}

function gameFunction()
{
    var computerChoice = Math.random();

    if (0 < computerChoice < 0.33)
    {
        computerChoice = "rock";
    }
    else if (0.33 < computerChoice < 0.67)
    {
        computerChoice = "scissors";
    }
    else
    {
        computerChoice = "paper";
    }

    console.log("Computer choice: ",computerChoice)

    if (userChoice === computerChoice) {
        console.log( "The result is a tie! Enter a new result?");
        startGame();
    } else if (userChoice === "rock") {
        if (computerChoice === "scissors")
        {
            console.log( "rock wins");
        }
        else if (computerChoice === "paper")
        {
            console.log( "paper wins");
        }
    } else if (userChoice === "paper") {
        if (computerChoice === "rock") {
            console.log( "paper wins");
        } else if (computerChoice === "scissors") {
            console.log ("scissors win");
        }
    } else if (userChoice === "scissors") {
        if (computerChoice === "paper") {
            console.log ("scissors wins");
        } else if (computerChoice === "rock") {
            console.log( "rock win");
        }
    }
}

startGame();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.