Home > Enterprise >  Rock Paper Scissors help (JavaScript)
Rock Paper Scissors help (JavaScript)

Time:01-01

Currently working on TOP rock paper scissors game and I'm having some trouble. The function playRound is showing up as undefined in the console. I'm not sure why as all my other function are working. Maybe i did something wrong in the other functions but I'm not sure. I feel like I'm so close yet so far from this thing working. Maybe it's just common beginner mistakes not sure. Any help would be appreciated.

function computerPlay() {
    var pickRandom = ["Rock", "Paper", "Scissors"];
    var randomMove = pickRandom[Math.floor(Math.random() * 3)];

    if (randomMove == "Rock") {
        randomMove.value = 0;
    } else if (randomMove == "Paper") {
        randomMove.value = 1;
    } else if (randomMove == "Scissors") {
        randomMove.value = 2;
    }
    return randomMove;
}

console.log(computerPlay());


var playerSelection = prompt("pick rock paper or scissors");

function userPlay() {

    if (playerSelection == "rock") {
        playerSelection.value = 0;
    } else if (playerSelection == "paper") {
        playerSelection.value = 1;
    } else if (playerSelection == "scissors") {
        playerSelection.value = 2;
    }
    return playerSelection;
}

console.log(userPlay());

function playRound(playerPick, computerSelection) {
    if (playerPick == 0 && computerSelection == 2) {
        alert("you win!!!");
    } else if (playerPick == 0 && computerSelection == 1) {
        alert("you lose hahahahaha!!!");
    } else if (playerPick == computerSelection) {
        alert("its a tie");
    }

}

const playerPick = userPlay();
const computerSelection = computerPlay();
console.log(playRound(playerPick, computerSelection));

CodePudding user response:

You shouldn't run it in console.log, as it doesn't have a return keyword, so it will return undefined. Just run it normally and it will be fine.

CodePudding user response:

You are close. I cleaned it up a bit, added a function that maps variants of human input to a normalized value, and replaced the (incomplete) conditions in playRound with an object that captures all of the win conditions in an object (see beats)...

function normalizePlay(play) {
  const variants = { r: 'rock', rock: 'rock', p: 'paper', paper: 'paper', s: 'scissors', scissors: 'scissors' };
  return variants [play.toLowerCase()];
}

function computerPlay() {
    var pickRandom = ["rock", "paper", "scissors"];
    return pickRandom[Math.floor(Math.random() * 3)];
}

function playRound(human, computer) {
   const beats = { rock: 'scissors', scissors: 'paper', paper: 'rock' }
    let result;
    if (human === computer) {
      result = 'tie';
    } 
    else if (beats[human] === computer) {
      result = 'you win';
    }
    else result = 'computer wins';
    console.log(`you picked ${human}, computer picked ${computer}... ${result}`)
}

const human = normalizePlay(prompt("pick rock paper or scissors (or r, p, s)"));
const computer = computerPlay();
playRound(human, computer);

CodePudding user response:

function computerPlay() {
  var pickRandom = ["Rock", "Paper", "Scissors"];
  var randomMove = pickRandom[Math.floor(Math.random() * 3)];

  if (randomMove == "Rock") {
    return 0;
  } else if (randomMove == "Paper") {
    return 1;
  } else if (randomMove == "Scissors") {
    return 2;
  }
}



var playerSelection = prompt("pick Rock, Paper or Scissors");

function userPlay() {

  if (playerSelection == "Rock") {
    return 0;
  } else if (playerSelection == "Paper") {
    return 1;
  } else if (playerSelection == "Scissors") {
    return 2;
  }
}


function playRound(playerPick, computerSelection) {
  console.log("player", playerPick)
  console.log("computerSelection", computerSelection)
  if (playerPick == 0 && computerSelection == 2) {
    console.log("you win!!!");
  } else if (playerPick == 0 && computerSelection == 1) {
    console.log("you lose hahahahaha!!!");
  } else if (playerPick == computerSelection) {
    console.log("its a tie");
  }
}

const playerPick = userPlay();
const computerSelection = computerPlay();
playRound(playerPick, computerSelection);

You you just needed to return the numbers from computerPlay and userPlay. Also, be careful with case sensitivity when using the prompt.

  • Related