rock paper scissors with two players in javascript - javascript

Hy there. I am new to javascript and i want to make a simple game of rock, papper scissors but after running the code i get the 2 prompted messages and the 'TypeError: playerOneChoice is not a function'. what did I do wrong ?
thanks very much.
function getPlayerOneChoice(){
var playerOneInput = prompt('Mihai what do you pick ?');
playerOneInput = playerOneInput.toLowerCase();
if(playerOneInput === 'rock' || playerOneInput === 'paper' || playerOneInput === 'scissors'){
return playerOneInput;
} else {
console.log('Invalid Choice !!!');
}
}
function getPlayerTwoChoice(){
var playerTwoInput = prompt('Flavia what do you pick ?');
playerTwoInput = playerTwoInput.toLowerCase();
if(playerTwoInput === 'rock' || playerTwoInput === 'paper' || playerTwoInput === 'scissors'){
return playerTwoInput;
} else {
console.log('Invalid pick !!');
}
}
function determineWinner(playerOneChoice, playerTwoChoice) {
if(playerOneChoice = playerTwoChoice) {
console.log('Tie');
}
if(playerOneChoice === 'rock'){
if(playerTwoChoice === 'scissors'){
return 'Mihai you won !';
} else {
return 'Flavia you won';
}
}
if(playerOneChoice === 'scissors'){
if(playerTwoChoice === 'paper'){
return 'Mihai you won!!!';
} else{
return 'Flavia you won!!!';
}
}
if(playerOneChoice === 'paper'){
if(playerTwoChoice === 'rock'){
return 'Mihai you won';
} else {
return 'Flavia you won';
}
}
}
function playGame() {
var playerOneChoice = getPlayerOneChoice();
var playerTwoChoice = getPlayerTwoChoice();
console.log('Mihai\'s pick : ' + playerOneChoice());
console.log('Flavia\'s pick : ' + playerTwoChoice());
console.log(determineWinner());
}
playGame();

There are 3 errors in your code
1. console.log('Mihai\'s pick : ' + playerOneChoice());
You are calling the function playerOneChoice which does not exist. You actually want to call the variable playerOneChoice. Remove the parenthesis.
2. console.log(determineWinner());
You are calling the method without parameter but you need both choice. Call it like determineWinner(playerOneChoice,playerTwoChoice)
3. if (playerOneChoice === playerTwoChoice)
You are assignin playerTwoChoice to playerOneChoice instead of comparing them. Use ===
function getPlayerOneChoice() {
var playerOneInput = prompt('Mihai what do you pick ?');
playerOneInput = playerOneInput.toLowerCase();
if (playerOneInput === 'rock' || playerOneInput === 'paper' || playerOneInput === 'scissors') {
return playerOneInput;
} else {
console.log('Invalid Choice !!!');
}
}
function getPlayerTwoChoice() {
var playerTwoInput = prompt('Flavia what do you pick ?');
playerTwoInput = playerTwoInput.toLowerCase();
if (playerTwoInput === 'rock' || playerTwoInput === 'paper' || playerTwoInput === 'scissors') {
return playerTwoInput;
} else {
console.log('Invalid pick !!');
}
}
function determineWinner(playerOneChoice, playerTwoChoice) {
if (playerOneChoice === playerTwoChoice) {
console.log('Tie');
}
if (playerOneChoice === 'rock') {
if (playerTwoChoice === 'scissors') {
return 'Mihai you won !';
} else {
return 'Flavia you won';
}
}
if (playerOneChoice === 'scissors') {
if (playerTwoChoice === 'paper') {
return 'Mihai you won!!!';
} else {
return 'Flavia you won!!!';
}
}
if (playerOneChoice === 'paper') {
if (playerTwoChoice === 'rock') {
return 'Mihai you won';
} else {
return 'Flavia you won';
}
}
}
function playGame() {
var playerOneChoice = getPlayerOneChoice();
var playerTwoChoice = getPlayerTwoChoice();
console.log('Mihai\'s pick : ' + playerOneChoice);
console.log('Flavia\'s pick : ' + playerTwoChoice);
console.log(determineWinner(playerOneChoice,playerTwoChoice));
}
playGame();

Remove () from playerOneChoice() and playerTwoChoice()
It must be so:
console.log('Mihai\'s pick : ' + playerOneChoice);
console.log('Flavia\'s pick : ' + playerTwoChoice);

Related

Beginners first try at coding rock, paper.. game. Keeps returning 'paper wins'

I keep getting 'paper wins' when I enter 'rock' as my choice.
function game() {
pChoice = prompt('What is your play?');
cChoice = ['rock','paper','scissor'];
let compChoice = [Math.floor(Math.random() * cChoice.length)];
console.log(compChoice);
function compare(pChoice, compChoice) {
if (pChoice === compChoice) {
alert( "It's a tie" );
}
if (pChoice === 'rock') {
if (compChoice === 'scissor') {
return 'rock wins';
} else {
return 'paper wins';
}
}
else if (pChoice === 'paper') {
if (compChoice === 'rock') {
return 'paper wins';
} else {
return 'scissor wins';
}
}
else if (pChoice === 'scissor') {
if (compChoice === 'paper') {
return 'scissor wins';
} else {
return 'rock wins';
}
}
}
var result = compare(pChoice, compChoice);
console.log(compare(pChoice, compChoice));
}
game();
You never assign cChoice to a value, instead you are assigning it to an array with a number in it.
For example:
// Assigning to [number]
let compChoice = [Math.floor(Math.random() * cChoice.length)];
// Assigning to string inside of array cChoice
let compChoice = cChoice[Math.floor(Math.random() * cChoice.length)];

why is my rock,paper game not running correctly

I'm learning front end and this project for a rock paper game is killing me, I'm not getting any errors and the logic seems correct to me but I'm not getting any output. I think my buttons are wrong and maybe I'm but I honestly have no clue id really appreciate it if somebody could help me out I have been stuck on this for hours.
<script>
const score=document.querySelector("score");
const buttons=document.querySelectorAll("buttons");
const computer=document.querySelector("computer");
const player=document.querySelector("player");
const rock=document.getElementById("rock");
const paper=document.getElementById("paper");
const scisscors=document.getElementById("scisscors");
function computerplayer(){
let computer=Math.floor(Math.random() * 3);
let random=['rock',"paper",'sisscors']
if(computer>3){
return 'rock'
}
if(computer<2){
return 'paper'
}
else{
return 'sisscors'
}
}
function game(choice,computer){
if(choice=='rock'&& computer=='sisscors'){
pscore++
score();
console.log( 'u win');
}
if(choice== 'rock'&& computer==="rock"){
console.log("it be a tie")
}
if(choice=="rock"&& computer=="paper"){
cscore++
score();
console.log("u lose");
}
if(choice== 'sisscors'&& computer=='rock'){
console.log( "u lost")
cscore++
score();
}
if(choice=='sisscors'&& computer=='paper'){
console.log('you win')
pscore++
score();
}
if(choice=='paper'&& computer=='rock'){
console.log("you win")
pscore++
score();
}
if(choice=='paper'&& computer=='sisscors'){
console.log("lost")
cscore++
score();
}
}
function score(){
if(pscore==3){
console.log("end game win")
}
if(cscore==3){
console.log("end game loser")
}
}
buttons.addEventListener('click', function(){
game('rock')
})
buttons.addEventListener('click', function(){
game('paper')
})
buttons.addEventListener('click', function(){
game('sisscors')
})
</script>
Thanks
First of all this code won't return 3 or greater number
let computer = Math.floor(Math.random() * 3);
then computerplayer() will only return 'paper' and 'sisscors' and you should change computerplayer() function.
Next you should change your buttons event listennr to these:
rock.addEventListener('click', function () {
game('rock', computerplayer())
})
paper.addEventListener('click', function () {
game('paper', computerplayer())
})
rock.addEventListener('click', function () {
game('sisscors', computerplayer())
})
Since you have to change many more things, then I mentioned a complete code here:
<script>
window.addEventListener('load', () => {
let pscore = 0;
let cscore = 0;
const result = document.querySelector(".result");
const player = document.querySelector(".pscore");
const computer = document.querySelector(".cscore");
const rock = document.getElementById("rock");
const paper = document.getElementById("paper");
const scissors = document.getElementById("scissors");
rock.addEventListener('click', function () {
game('rock', computerplayer())
})
paper.addEventListener('click', function () {
game('paper', computerplayer())
})
scissors.addEventListener('click', function () {
game('scissors', computerplayer())
})
function computerplayer() {
let random = Math.floor(Math.random() * 3);
if (random == 0)
return 'rock'
else if (random == 1)
return 'paper'
else if (random == 2)
return 'scissors'
}
function game(choice, computerAction) {
if (choice == 'rock' && computerAction == 'scissors') {
pscore++
score();
console.log('u win');
} else if (choice == 'rock' && computerAction === "rock") {
console.log("it be a tie")
} else if (choice == "rock" && computerAction == "paper") {
cscore++
score();
console.log("u lose");
} else if (choice == 'scissors' && computerAction == 'rock') {
console.log("u lost")
cscore++
score();
} else if (choice == 'scissors' && computerAction == 'paper') {
console.log('you win')
pscore++
score();
} else if (choice == 'scissors' && computerAction === "scissors") {
console.log("it be a tie")
} else if (choice == 'paper' && computerAction == 'rock') {
console.log("you win")
pscore++
score();
} else if (choice == 'paper' && computerAction == 'scissors') {
console.log("lost")
cscore++
score();
} else if (choice == 'paper' && computerAction === "paper") {
console.log("it be a tie")
}
function score() {
console.log(pscore, cscore)
player.innerHTML = pscore;
computer.innerHTML = cscore;
if (pscore == 3) {
console.log("end game win")
result.innerHTML = 'You won.';
} else if (cscore == 3) {
console.log("end game loser")
result.innerHTML = 'Computer won.';
}
if (pscore == 3 || cscore == 3) {
rock.style.display = 'none';
paper.style.display = 'none';
scissors.style.display = 'none';
}
}
}
});
</script>
<body>
Computer:<span class="cscore"></span>
<br />
Player: <span class="pscore"></span><br />
<br />
<input type="button" id="rock" value="Rock" />
<input type="button" id="paper" value="Paper" />
<input type="button" id="scissors" value="scissors" />
<span class="result"></span>
</body>

How to add error alert if provided choice is none of rock, paper, scissors? How to make the whole code shorter?

How to add error alert if provided choice is none of rock, paper, scissors? How to make the whole code shorter?
function game (inputOne, inputTwo){
function result(inputOne, inputTwo){
if (inputOne === inputTwo){
return '0';
}
if (inputOne === 'rock'){
if (inputTwo === 'scissors'){
return '2';
} else {
return '1';
}
}
if (inputOne === 'paper'){
if (inputTwo === 'rock'){
return '2';
} else {
return '1';
}
}
if (inputOne === 'scissors'){
if (inputTwo === 'paper'){
return '2';
} else {
return '1';
}
}
}
function checkResult(){
if (result(inputOne, inputTwo) === '0'){
return 'Tie!';
}
if (result(inputOne, inputTwo) === '1'){
return 'Player 2 won!'
}
if (result(inputOne, inputTwo) === '2'){
return 'Player 1 won!';
}
}
return checkResult();
}
console.log(game('rock', 'paper'));
When I try to write the code shorter like per example below, function is not working correctly:
if (inputOne === 'scissors' && inputTwo === 'paper'){
return '2';
} else {
return '1';
}
}
This is a suggestion to make the code shorter:
function game(inputOne, inputTwo) {
const rules = {
rock: 'scissors',
paper: 'rock',
scissors: 'paper',
};
if (inputOne === inputTwo) {
return 'Tie';
}
return rules[inputOne] === inputTwo ? 'Player 1 won!' : 'Player 2 won!';
}
const player1 = prompt('Player 1 choice: ');
const player2 = prompt('Player 2 choice: ');
console.log(game(player1, player2));
To handle invalid input you may do this:
function game(inputOne, inputTwo) {
const rules = {
rock: 'scissors',
paper: 'rock',
scissors: 'paper',
};
const valid = ['rock', 'paper', 'scissors'];
if (!valid.includes(inputOne) || !valid.includes(inputOne)) {
const errorMessage = 'Invalid input!';
alert(errorMessage);
return errorMessage;
}
if (inputOne === inputTwo) {
return 'Tie';
}
return rules[inputOne] === inputTwo ? 'Player 1 won!' : 'Player 2 won!';
}
const player1 = prompt('Player 1 choice: ');
const player2 = prompt('Player 2 choice: ');
console.log(game(player1, player2));
I return the errorMessage because a string is expected.

Tie result cannot be outputted

While creating the rock, paper, scissors application, I am tasked to output specific results in the console, such as outputting paper-paper and receive 'It's a tie' result. Well, I have tried to do that but for some reason, I cannot do so. The code is as below:
const user = userInput => {
userInput = userInput.toLowerCase();
if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
return userInput;
} else {
console.log('error');
}
}
function computer() {
random = Math.floor(Math.random() * 3);
switch(random){
case 0 : return 'rock';
break;
case 1 : return 'paper';
break;
default : return 'scissors';
}
};
const winner = () => {
const choice1 = user('rock');
const choice2 = computer();
if(choice1 === choice2) {
return 'It\'s a tie!';
}
if(choice1 === 'rock') {
if(choice2 === 'paper') {
return 'Computer wins!';
} else {
return 'You win!';
}
}
if(choice1 === 'paper') {
if(choice2 === 'scissors') {
return 'Computer wins!';
} else {
return 'You win!';
}
}
if(choice1 === 'scissors') {
if(choice2 === 'rock') {
return 'Computer wins!';
} else {
return 'You win!';
}
}
}
console.log(winner('paper', 'paper'));
When I try to type console.log(winner('paper', 'paper')) for example, the result is random meaning that it can be either win, lost or tie due to the fact (or what I suppose is the reason) that the computer's choice is random but this is how I am asked to create the computer's choice. If I type console.log(('paper', 1)) I get the same result but somehow, the task is asking me to use console.log(winner('paper', 'paper')) and is telling me that the result should be a tie. I have also tried to let the user with no parameter in choice1 but I get an error due to the fact that there is no input in the user's choice.
The tasks are as below:
console.log(determineWinner('paper', 'scissors')); // prints something like 'The computer won!'
console.log(determineWinner('paper', 'paper')); // prints something like 'The game is a tie!'
console.log(determineWinner('paper', 'rock')); // prints something like 'The user won!'
Any help or idea would be appreciated. Thanks!
Something like this would work. If It is what you need.
const user = userInput => {
userInput = userInput.toLowerCase();
if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
return userInput;
} else {
console.log('error');
}
}
function computer() {
const random = Math.floor(Math.random() * 3);
switch(random){
case 0 : return 'rock';
break;
case 1 : return 'paper';
break;
default : return 'scissors';
}
};
// Here is where you need to change something
const determineWinner = (userChoice, computerChoice) => {
// And use the parameters here:
const choice1 = userChoice;
const choice2 = computerChoice;
if (choice1 === choice2) {
return 'It\'s a tie!';
}
if (choice1 === 'rock') {
if (choice2 === 'paper') {
return 'Computer wins!';
} else {
return 'You win!';
}
}
if (choice1 === 'paper') {
if (choice2 === 'scissors') {
return 'Computer wins!';
} else {
return 'You win!';
}
}
if (choice1 === 'scissors') {
if (choice2 === 'rock') {
return 'Computer wins!';
} else {
return 'You win!';
}
}
};
const userInput = 'rock';
console.log(determineWinner(user(userInput), computer()));
console.log(determineWinner('rock', 'paper'));
console.log(determineWinner('paper', 'paper'));
// For you requirement:
console.log(determineWinner('paper', 'scissors'));
console.log(determineWinner('paper', 'paper'));
console.log(determineWinner('paper', 'rock'));
But if you hardcode the parameters as you requirement suggests then your functions user and computer are not used.

Javascript throws undefined

So i've been trying to learn to code and have been using codeAcademy as as resource. I'm currently doing a Rock, Paper, Scissors exercise and my code runs fine but my last console.log(determineWinner(userChoice, computerChoice));" throws an undefined into the console. Any help would be appreciated.
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else {
console.log('Error!');
}
}
const getComputerChoice = () => {
switch (Math.floor(Math.random() * 3)) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
const determineWinner = (userChoice, computerChoice) => {
if (getUserChoice === getComputerChoice) {
return 'the game was a tie';
}
}
if (getUserChoice === 'rock') {
if (getComputerChoice === 'paper') {
return 'computer won!';
} else {
return 'you won!';
}
}
if (getUserChoice === 'paper') {
if (getComputerChoice === 'scissors') {
return 'computer won!';
} else {
return 'you won!';
}
}
if (getUserChoice === 'scissors') {
if (getComputerChoice === 'rock') {
return 'computer won!';
} else {
return 'you won!';
}
}
const playGame = () => {
const userChoice = getUserChoice('rock');
const computerChoice = getComputerChoice();
console.log(`You threw: ${userChoice}`);
console.log(`The computer threw: ${computerChoice}`);
console.log(determineWinner(userChoice, computerChoice));
};
playGame();
Ignoring a small syntax mistake in the code you posted here, the problem with the non-working code is that in the function determineWinner, you've two variables named userChoice and computerChoice. But, wrongly, you are using getUserChoice and getComputerChoice.
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else {
console.log('Error!');
}
}
const getComputerChoice = () => {
switch (Math.floor(Math.random() * 3)) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'the game was a tie';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'computer won!';
} else {
return 'you won!';
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'computer won!';
} else {
return 'you won!';
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'computer won!';
} else {
return 'you won!';
}
}
}
const playGame = () => {
const userChoice = getUserChoice('rock');
const computerChoice = getComputerChoice();
console.log(`You threw: ${userChoice}`);
console.log(`The computer threw: ${computerChoice}`);
console.log(determineWinner(userChoice, computerChoice));
};
playGame();

Categories

Resources