0

Why isn't this working properly

the output is wrong as an example, when computer brings out rock and the user paper it says it is a tie here's my code //Rock paper scissors is a classic two player game. Each player chooses either rock, paper or scissors. The items are compared, and whichever player chooses the more powerful item wins. // UserInput is defined based on user's decision var userInput = 'rock'; userInput = userInput.toLowerCase(); //TheComputerChoice give a number between one, two and three.Based on the outcome number, it returns rock, paper or scissors function getComputerChoice() { x = Math.floor(Math.random() * 2); if (x === 0) return 'rock'; else if (x === 1) return 'paper'; else if (x === 2) return 'scissors'; } // This function determines the winner by doing some simple math function determineWinner(userChoice,computerChoice) { if (userChoice === computerChoice) return 'tie'; else if (userChoice === 'rock' && computerChoice === 'paper') return 'computer won'; else if (userChoice === 'paper' && computerChoice === 'rock') return 'user won'; else if ( userChoice === 'scissors' && computerChoice === 'rock') return 'computer won'; else if ( userChoice === 'rock' && computerChoice === 'scissors') return 'user won'; else if ( userChoice === 'paper' && computerChoice === 'scissors') return 'computer won'; else if ( userChoice === 'scissors' && computerChoice === 'paper') return 'user won'; else if ( userChoice === 'bomb') return 'user won'; } // In this function. userChoice and computerChoice were defined in order to use console.logs function playGame() { var userChoice = userInput; var computerChoice = getComputerChoice(); console.log(determineWinner(userChoice,computerChoice)); } // the results are printed out console.log('userInput:' + userInput); console.log('getComputerChoice:' + getComputerChoice()); playGame();

10th Jan 2018, 6:25 PM
Eilia Zandi
Eilia Zandi - avatar
2 Respuestas
+ 2
You are calling your getComputerChoice() function twice. 1st time: console.log('getComputerChoice:' + getComputerChoice()); 2nd time within this function playGame(); Given computer choice is random the 1st result may be different to the 2nd so the choice printed may not be the same as the choice in the game.
10th Jan 2018, 7:44 PM
Duncan
Duncan - avatar
0
You have two mistakes: + getComputerChoice() never return 'scissors' until you change: x = Math.floor(Math.random() * 3); // will now produce integer in [0;3[ (0 included, 3 not included) + you log a different computer choice than the one computed for determine winner: put your log (at least the computer choice one) inside your playGame() function, and compute only once the computer choice: function playGame() { var userChoice = userInput; var computerChoice = getComputerChoice(); console.log('getComputerChoice:' + computerChoice); console.log(determineWinner(userChoice,computerChoice)); } // the results are printed out console.log('userInput:' + userInput); //console.log('getComputerChoice:' + getComputerChoice()); playGame();
10th Jan 2018, 7:34 PM
visph
visph - avatar