Javascript rock paper scissors project

Try this corrected and verified JavaScript Code –

var rock = rock;
var paper= paper;
var snip = snip;

var playerOneName= prompt("what is your name");
playerOne = choice();
var playerTwoName = prompt("what is your name");
playerTwo=choice();

alert('Player 1 Chose  - '+ playerOne + 'Player 2 Chose  - '+playerTwo);

function choice(pick){
    return(prompt("rock paper or snip"));  //return the prompt value
}

if(playerOne === 'rock' && playerTwo === 'snip'){
    alert(playerOneName + ' ' + 'Wins');
}else if(playerOne === 'paper' && playerTwo === 'rock'){
    alert(playerOneName + ' ' + 'Wins');
}else if(playerOne === 'snip' && playerTwo === 'paper'){
    alert(playerOneName + ' ' + 'Wins');
}else if(playerTwo === 'rock' && playerOne === 'snip'){
   alert(playerTwoName + ' ' + 'Wins');
}else if(playerTwo === 'paper' && playerOne === 'rock'){
    alert(playerTwoName + ' ' + 'Wins');
}else if(playerTwo === 'snip' && playerOne === 'paper'){
   alert(playerTwoName + ' ' + 'Wins');
}else if(playerOne === playerTwo){
    alert('its a tie try again');
}

Corrections

  1. Return value in choice() function. Prompt has the return value which is to be received and returned in the function.
  2. Alert and check the values for debugging purpose. Always try this to know whats happening inside the code.

Leave a Comment