OpenTheCSV
OpenTheCSV

Reputation: 93

Javascript function does not run when called

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?

Upvotes: 0

Views: 108

Answers (6)

lordkain
lordkain

Reputation: 3109

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

Upvotes: 0

Saroj
Saroj

Reputation: 1571

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.

Upvotes: 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

Upvotes: 0

user1567453
user1567453

Reputation: 2095

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

Upvotes: 1

Nguyen Tuan Anh
Nguyen Tuan Anh

Reputation: 1035

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

Upvotes: 0

Deepak Sharma
Deepak Sharma

Reputation: 1137

No line executed after return statement.. try

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

Upvotes: 1

Related Questions