0

I'm just starting out and I'm having issues building a RPS program. I'm supposed to be following the directions provided in hidden comments, I just dont understand what I'm supposed to be doing with the 2 functions getPlayerMove(move) and getComputerMove(move).

I was able to get the program to work if loaded into an index.html and played through the console in the browser but it doesn't work otherwise (say, with repl http://repl.it/hUa/8 )

// ////////////////////////////////////////////////
/*   Provided Code - Please Don't Edit   */
////////////////////////////////////////////////
'use strict';

playToFive();

function getInput() {
    console.log("Please choose either 'rock', 'paper', or 'scissors'.");
    return prompt();
}

function randomPlay() {
    var randomNumber = Math.random();
    if (randomNumber < 0.33) {
        return "rock";
    } else if (randomNumber < 0.66) {
        return "paper";
    } else {
        return "scissors";
    }
}
////////////////////////////////////////////////
/*           Write Your Code Below            */
////////////////////////////////////////////////

var playerMove;
var computerMove;

function getPlayerMove(move) {
    // Write an expression that operates on a variable called `move`
    var move = getInput();
    if (move === null) {
        getInput();
    } else {
        console.log("player chooses " + move);
        playerMove = move;
        getComputerMove();
    } 

}

function getComputerMove(move) {
    var move = randomPlay();
    if (move === null || 0) {
        randomPlay();
    } else {
        console.log("computer chooses " + move);
        computerMove = move;
        getWinner(playerMove, computerMove);
    }
}

var winner;
var getWinner = function(playerMove, computerMove) {
    //var winner;
    // Write code that will set winner to either 'player', 'computer', or 'tie' based on the values of playerMove and computerMove.
    // Assume that the only values playerMove and computerMove can have are 'rock', 'paper', and 'scissors'.
    // The rules of the game are that 'rock' beats 'scissors', 'scissors' beats 'paper', and 'paper' beats 'rock'.
    /* YOUR CODE HERE */
    if (playerMove === computerMove) {
        winner = "tie";
        console.log("Looks like we have a tie!");
        return winner;
    } 
    else if (playerMove === "rock") {
        if (computerMove === "paper") {
            winner = "computer";
        } else {
            winner = "player";
        }
    } else if (playerMove === "paper") {
        if (computerMove === "rock") {
            winner = "player";
        } else {
            winner = "computer";
        }
    } else if (playerMove === "scissors") {
        if (computerMove === "rock") {
            winner = "computer";
        } else {
            winner = "player";
        }
    } else {
        getInput();
    }
    console.log("Looks like the " + winner + " wins!");
    return winner;
}

function playToFive() {
    console.log("Let's play Rock, Paper, Scissors");
    var playerWins = 0;
    var computerWins = 0;
    for (var i=0; i<25; i++) {
        if (playerWins === 5 || computerWins === 5) {
            console.log("FINAL SCORE: Player Wins : " + playerWins + "  Computer Wins : " + computerWins);
            break;
        } 
        else 
        {
            if (winner === "player") 
            {
             playerWins ++;
             console.log("Player Wins : " + playerWins + "  Computer Wins : " + computerWins);
            } 
            else if (winner === "computer") 
            {
             computerWins ++;
             console.log("Player Wins : " + playerWins + "  Computer Wins : " + computerWins);
            }
        getPlayerMove();    

        }
    }
}

3 Answers3

0

Where you define the getWinner() function you say

var getWinner = function(playerMove, computerMove) { ... }

That will work in the browser because the default scope of variables is the window object. So variables declared out of defined context are assigned to the window object.

When run in a terminal like that, I'm not sure what the default scope is (or if there even is one) so that function may not exist after its declaration.

The solution that worked for me was changing the above line to

function getWinner(playerMove, computerMove) { ... }

Hopefully that works for you. I'll try and find more information and will update when I have a more concrete explanation for what's happening.

EDIT: Read more about the difference between function Declaration and function Expression Declaration vs Expression

Community
  • 1
  • 1
isick
  • 1,283
  • 1
  • 10
  • 21
  • thank you and thanks for the reference materials. Makes way more sense now –  Apr 07 '15 at 22:27
0

You are setting the variable getWinner later in the code than when it is called. Try moving it's declaration before it is called.

Big_Mac
  • 2,634
  • 3
  • 17
  • 32
  • it looks like isick's explanation works. I tried moving the function declaration above where the function is called but that didn't seem to make a difference, I thought it would hoist where the function was introduced above where it was called in another function but I could be wrong. –  Apr 07 '15 at 22:24
0

I've got it working here, but now just have to finesse the part with the prompt.

The player's move is now hard coded - all you now need to do is get this via the prompt function and it all works.

// ////////////////////////////////////////////////
/*   Provided Code - Please Don't Edit   */
/*   IT WORKS!!!          ///////////// */
//////////////////////////////////////////////////
'use strict';

rockPaperScissors();


function randomPlay() {
    var randomNumber = Math.random();
    if (randomNumber < 0.33) {
        return "rock";
    } else if (randomNumber < 0.66) {
        return "paper";
    } else {
        return "scissors";
    }
}
////////////////////////////////////////////////
/*           Write Your Code Below            */
////////////////////////////////////////////////

var playerMove;
var computerMove;
var winner;

function getComputerMove() {
    var move = randomPlay();
    if (move === null || 0) {
        randomPlay();
    } else {
        console.log("The computer chose " + move);
        computerMove = move;
    }
}

function getPlayerMove() {
    // Write an expression that operates on a variable called `move`
    //////////////////// The PLAYER move is hard coded in the line below this one///////////////////////////
    var move = 'scissors';
        console.log("The player chose " + move);
        playerMove = move;
}



function rockPaperScissors() {
    console.log("Let's play Rock, Paper, Scissors.");
    getComputerMove();
    getPlayerMove();   
    getWinner();
}

function getWinner() {
    var winner;
  if ((computerMove === 'rock') && (playerMove === 'rock')) {
     winner = "It's a tie!";
 } else if ((computerMove === 'scissors') && (playerMove === 'rock')) {
    winner = "Player. Congratulations you Win!!!";
} else if ((computerMove === 'paper') && (playerMove === 'rock')) {
     winner = "The computer. You lose bitch!";
  } else if ((computerMove === 'rock') && (playerMove === 'scissors')) {
     winner = "The computer. You lose bitch!";
 } else if ((computerMove === 'scissors') && (playerMove === 'scissors')) {
    winner = "It's a tie!";
} else if ((computerMove === 'paper') && (playerMove === 'scissors')) {
     winner = "The player. You win!";
  }else if ((computerMove === 'rock') && (playerMove === 'paper')) {
     winner = "The player. You win!";
 } else if ((computerMove === 'scissors') && (playerMove === 'paper')) {
    winner = "The computer. You lose bitch!";
} else if ((computerMove === 'paper') && (playerMove === 'paper')) {
     winner = "It's a tie!";
  }
console.log("The winner is: " + winner);
}

https://repl.it/BvcS/7

bummi
  • 26,435
  • 13
  • 58
  • 97