im trying to create the game "rock, paper or scissors" in JavaScript, but im stuck on the last function "game()", it should repeat 5 times the function playRound() and throw a result each of those 5 times. But doesnt work.
function computerPlay(){
let optionsList = ["rock", "paper", "scissors"];
let option = optionsList[Math.floor(Math.random() * optionsList.length)];
return option
}
function playRound(playerSelection, computerSelection){
var playerSelection = prompt("Choose: rock, paper or scissors").toLowerCase();
var computerSelection = computerPlay();
if (playerSelection = "rock"){
switch(computerSelection){
case "rock":
return "T";
case "paper":
return "M";
case "scissors":
return "Y";
}
}else if (playerSelection = "paper"){
switch(computerSelection){
case "rock":
return "Y";
case "paper":
return "T";
case "scissors":
return "M";
}
}else if (playerSelection = "scissors"){
switch(computerSelection){
case "rock":
return "M";
case "paper":
return "Y";
case "scissors":
return "T";
}
}
}
function game(){
let i = 0
for(i=1; i<=5; i++){
playRound()
if (playRound()= "M") {
console.log("Machine Wins");
}else if (playRound() = "Y"){
console.log("You Win")
}else if (playRound() = "T"){
console.log("You Win!")
}
}
}
You have a couple errors:
The first is that you are trying to use the = operator to compare values. You need to use == or ===.
Secondly, you need to get the return value of playRound() and then check its value to determine the outcome of the game. But instead, you are calling playRound() initially in each iteration of your loop, and then again each time you try to determine the outcome.
Try:
function computerPlay() {
let optionsList = ["rock", "paper", "scissors"];
let option = optionsList[Math.floor(Math.random() * optionsList.length)];
return option
}
function playRound(playerSelection, computerSelection) {
var playerSelection = prompt("Choose: rock, paper or scissors").toLowerCase();
var computerSelection = computerPlay();
if (playerSelection === "rock") {
switch (computerSelection) {
case "rock":
return "T";
case "paper":
return "M";
case "scissors":
return "Y";
}
} else if (playerSelection === "paper") {
switch (computerSelection) {
case "rock":
return "Y";
case "paper":
return "T";
case "scissors":
return "M";
}
} else if (playerSelection === "scissors") {
switch (computerSelection) {
case "rock":
return "M";
case "paper":
return "Y";
case "scissors":
return "T";
}
}
}
function game() {
let i = 0
for (i = 1; i <= 5; i++) {
let outcome = playRound()
if (outcome === "M") {
console.log("Machine Wins");
} else if (outcome === "Y") {
console.log("You Win")
} else if (outcome === "T") {
console.log("You Win!")
}
}
}
game();
Not sure it this fixes the problem but here's a syntax error: = instead of ==. Also, you shouldn't call playRound() in every else. Just store the result in a variable.
let result = playRound()
if (result== "M") {
console.log("Machine Wins");
}else if (result == "Y"){
console.log("You Win")
}else if (result == "T"){
console.log("You Win!")
}
Related
The first function getComputerChoice() returns a random - rock, paper, or scissor. However, in the playRound function when the computer' choice and user's choice are compared, it shows incorrect answers for a few of them. For example, when I choose rock and computer chooses paper, I win, which is wrong.
I tried using if else and switch statements but faced the same problem. It would be great if someone could help me out.
let computerSelection = getComputerChoice();
let playerSelection = prompt("rock, paper, or scissor?").toLowerCase();
function getComputerChoice(){
const randomNumber = Math.floor(Math.random()*3);
if(randomNumber === 0){
choice = "rock";
} else if(randomNumber === 1){
choice = "paper";
} else if(randomNumber === 2){
choice = "scissor";
} return choice;
}
function playRound(playerSelection, computerSelection){
let result = '';
if(computerSelection === playerSelection){
result = "It's a tie";
} else if(computerSelection === 'paper' && playerSelection === 'rock'){
result = "you lose";
} else if(computerSelection === 'scissor' && playerSelection === 'rock'){
result = "you win";
} else if ((computerSelection === 'rock') && (playerSelection === 'paper')){
result = "You win";
} else if((computerSelection === 'scissor') && (playerSelection === 'paper')){
result = "you lose";
} else if ((computerSelection === 'paper') && (playerSelection === 'scissor') ){
result = "You win";
} else if((computerSelection === 'rock') && (playerSelection === 'scissor')){
result = "You lose";
}
return result;
};
alert("The computer chose: " + getComputerChoice());
alert("That means " + playRound(playerSelection, computerSelection));
In your first statement, you choose the computer selection. But in your second to last statement, you call getComputerChoice() which makes a DIFFERENT selection. Change the next to the last line to
alert("The computer chose: " + computerSelection);
Try adding this debug line that I have put in, and then look at the console in the browser. You will see what the computer really chose
function getComputerChoice(){
const randomNumber = Math.floor(Math.random()*3);
if(randomNumber === 0){
choice = "rock";
} else if(randomNumber === 1){
choice = "paper";
} else if(randomNumber === 2){
choice = "scissor";
}
// debug line
console.log('randomly chose', choice);
return choice;
}
ChatGPT?
This code will run but it has a couple of problems:
In the getComputerChoice function, the variable choice is declared without const, let, or var. This means that it's a global variable and could cause issues in the future.
The line alert("The computer chose: " + getComputerChoice()); is calling the getComputerChoice function twice, but it should only be called once and stored in the computerSelection variable.
The result of the playRound function is not being logged to the console or displayed in any other way, it's only being returned.
To resolve these problems, you could modify the code as follows:
let computerSelection = getComputerChoice();
let playerSelection = prompt("rock, paper, or scissor?").toLowerCase();
function getComputerChoice(){
let choice = '';
const randomNumber = Math.floor(Math.random() * 3);
if(randomNumber === 0){
choice = "rock";
} else if(randomNumber === 1){
choice = "paper";
} else if(randomNumber === 2){
choice = "scissor";
}
return choice;
}
function playRound(playerSelection, computerSelection){
let result = '';
if(computerSelection === playerSelection){
result = "It's a tie";
} else if(computerSelection === 'paper' && playerSelection === 'rock'){
result = "you lose";
} else if(computerSelection === 'scissor' && playerSelection === 'rock'){
result = "you win";
} else if ((computerSelection === 'rock') && (playerSelection === 'paper')){
result = "You win";
} else if((computerSelection === 'scissor') && (playerSelection === 'paper')){
result = "you lose";
} else if ((computerSelection === 'paper') && (playerSelection === 'scissor') ){
result = "You win";
} else if((computerSelection === 'rock') && (playerSelection === 'scissor')){
result = "You lose";
}
return result;
};
console.log("The computer chose: " + computerSelection);
console.log("That means " + playRound(playerSelection, computerSelection));
Your Code is not consistent : here is the answer:
let computerSelection = getComputerChoice();
let playerSelection = prompt("rock, paper, or scissor?").toLowerCase();
function getComputerChoice(){
const randomNumber = Math.floor(Math.random() * 3);
let choice;
if(randomNumber === 0){
choice = "rock";
} else if(randomNumber === 1){
choice = "paper";
} else if(randomNumber === 2){
choice = "scissors";
}
return choice;
}
function playRound(playerSelection, computerSelection){
let result = '';
if(computerSelection === playerSelection){
result = "It's a tie";
} else if(computerSelection === 'paper' && playerSelection === 'rock'){
result = "You lose";
} else if(computerSelection === 'scissors' && playerSelection === 'rock'){
result = "You win";
} else if ((computerSelection === 'rock') && (playerSelection === 'paper')){
result = "You win";
} else if((computerSelection === 'scissors') && (playerSelection === 'paper')){
result = "You lose";
} else if ((computerSelection === 'paper') && (playerSelection === 'scissors') ){
result = "You lose";
} else if((computerSelection === 'rock') && (playerSelection === 'scissors')){
result = "You win";
}
return result;
};
let result = playRound(playerSelection, computerSelection);
alert("The computer chose: " + computerSelection);
alert("That means " + result);
Note:
scissor has been corrected to scissors.
Consistent use of capitalization for "You win" and "You lose".
The result ofplayRound() function has been stored in a variable
result before using it in the alert
.
I am planning to make a score. Whoever gets to 5 points of winning, wins the game.
So far, I have tried to use for loops to checkWinner() but it's not going as planned
After the winner has been decided, I would need to press play again to restart the whole game
cardText.forEach(button => button.addEventListener("click", () => {
player = button.textContent;
getComputerChoice();
playerScore.textContent = `Player: ${player}`;
computerScore.textContent = `Computer: ${computer}`;
resultText.textContent = checkWinner();
}));
function getComputerChoice() {
const randNum = Math.floor(Math.random() * 3) + 1;
switch (randNum) {
case 1:
computer = "Rock";
break;
case 2:
computer = "Paper";
break;
case 3:
computer = "Scissors";
break;
}
}
function checkWinner() {
if (player == computer) {
return "Draw";
} else if (computer == "Rock") {
return (player == "Paper") ? "You Win !" : "You Lose"
} else if (computer == "Paper") {
return (player == "Scissors") ? "You Win !" : "You Lose"
} else if (computer == "Scissors") {
return (player == "Rock") ? "You Win !" : "You Lose"
}
}
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));
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.
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);
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.
I`m currently stuck with a rock, paper, scissors game in javascript.
I need to display in console something like this:
Computer choice: “Rock”
User choice: “Paper”
User wins!
How can I implement this in the code below.
let userSelection = userPlay();
function userPlay() {
let random = ["rock", "paper", "scissors"];
return random[Math.floor(Math.random() * 3)];
}
let computerSelection = computerPlay();
function computerPlay() { //computer generates a random answer.
let random = ["rock", "paper", "scissors"];
return random[Math.floor(Math.random() * 3)];
}
function playRound(playerSelection, computerSelection) { //plays a round of the game.
if (playerSelection === "rock") {
if (computerSelection === "rock") {
return "Draw!";
} else if (computerSelection === "paper") {
return "Computer wins!";
} else {
return "User wins!";
}
} else if (playerSelection === "paper") {
if (computerSelection === "rock") {
return "User wins!";
} else if (computerSelection === "paper") {
return "Draw!";
} else {
return "Computer wins!";
}
} else {
if (computerSelection === "rock") {
return "Computer wins!";
} else if (computerSelection === "paper") {
return "User wins!";
} else {
return "Draw!";
}
}
}
console.log(playRound(userPlay, computerSelection));
I have swapped the position of function calls for better readability. Idea is to store the result of each function call into a variable and print it.
function userPlay() {
let random = ["rock", "paper", "scissors"];
return random[Math.floor(Math.random() * 3)];
}
function computerPlay() { //computer generates a random answer.
let random = ["rock", "paper", "scissors"];
return random[Math.floor(Math.random() * 3)];
}
function playRound(playerSelection, computerSelection) { //plays a round of the game.
if (playerSelection === "rock") {
if (computerSelection === "rock") {
return "Draw!";
} else if (computerSelection === "paper") {
return "Computer wins!";
} else {
return "User wins!";
}
} else if (playerSelection === "paper") {
if (computerSelection === "rock") {
return "User wins!";
} else if (computerSelection === "paper") {
return "Draw!";
} else {
return "Computer wins!";
}
} else {
if (computerSelection === "rock") {
return "Computer wins!";
} else if (computerSelection === "paper") {
return "User wins!";
} else {
return "Draw!";
}
}
}
var userChoice = userPlay();
var computerSelection = computerPlay();
var result = playRound(userChoice, computerSelection)
console.log("user's choice", userChoice);
console.log("computer's choice", computerSelection);
console.log("Result is", result);
I'm not sure if I'm too late for an answer but I have refactored the code a bit (it can be refactored a bit more I think, but I'll let you do that).
let playerSelection = getNumber();
let computerSelection = getNumber();
function getNumber() { //computer generates a random answer.
let random = ["rock", "paper", "scissors"];
return random[Math.floor(Math.random() * 3)];
}
function playRound(playerSelection, computerSelection) { //plays a round of the game.
let message = '';
let random = ["rock", "paper", "scissors"];
let playerSelectionIndex = random.indexOf(playerSelection);
let computerSelectionIndex = random.indexOf(computerSelection);
switch (true) {
case(playerSelectionIndex == computerSelectionIndex + 1):
// Scissors beats rock
case((playerSelectionIndex + 2) == computerSelectionIndex):
message = 'You Win';
break;
case (computerSelectionIndex == playerSelectionIndex):
message = 'Draw';
break;
default:
message = 'You lose';
break;
}
return message;
}
console.log(playRound(playerSelection, computerSelection));
**I am trying to create rock paper scissors in java script and The below is the code I am executing, It is showing "The match is draw! play again" everytime. What goes wrong?
First taking the user input and checking the correctness. Generating the computer input by Math.random and assigning them RPS based on the output. Finally comparing both to determine the winner.**
const getUserChoice = function(userInput){
userInput = userInput.toLowerCase();
if (userInput === 'rock'|| userInput ==='paper'|| userInput ==='scissors'){
return userInput;
}else {
console.log('Error');
}
}
userChoice = getUserChoice('rock');
console.log(getUserChoice('rock'));
const getComputerChoice = function(){
randomNumber = Math.floor(Math.random()*3);
switch(randomNumber) {
case 0:
return 'rock';
break;
case 1:
return 'paper';
break;
case 2:
return 'scissors';
break;
default:
return 'error';
}
}
computerChoice = getComputerChoice();
console.log(getComputerChoice());
const determineWinner = function(userChoice, computerChoice){
if (userChoice === computerChoice){
return 'The match is draw! Play again.';
}
if (userChoice === 'rock'){
if (computerChoice === 'paper'){
return 'you lost';
}else{
return 'you won';
}
}
if (userChoice === 'paper'){
if (computerChoice === 'rock'){
return 'you won';
}else{
return 'you lost';
}
}
if (userChoice === 'scissors'){
if (computerChoice === 'rock'){
return 'you lost';
}else{
return 'you won';
}
}
}
console.log(determineWinner());**strong text**
On your bottom line, you are not passing any parameters through.
Change it to:
console.log(determineWinner(userChoice, computerChoice));**strong text**
Currently, the first line of your determineWinner function is checking if null is equal to null (which it is), so is returning a draw.
Here you go. Checkout the below answer:
const getUserChoice = function(userInput){
userInput = userInput.toLowerCase();
if (userInput === 'rock'|| userInput ==='paper'|| userInput ==='scissors'){
return userInput;
}else {
console.log('Error');
}
}
const userChoice = getUserChoice('rock');
const getComputerChoice = function(){
const randomNumber = Math.floor(Math.random() * 3);
switch(randomNumber) {
case 0:
return 'rock';
break;
case 1:
return 'paper';
break;
case 2:
return 'scissors';
break;
default:
return 'error';
}
}
const computerChoice = getComputerChoice();
const determineWinner = function(userChoice, computerChoice){
console.log('user choice: ', userChoice);
console.log('computer choice: ', computerChoice);
if (userChoice === computerChoice){
return 'The match is draw! Play again.';
}
if (userChoice === 'rock'){
if (computerChoice === 'paper'){
return 'you lost';
} else {
return 'you won';
}
}
if (userChoice === 'paper'){
if (computerChoice === 'rock'){
return 'you won';
}else{
return 'you lost';
}
}
if (userChoice === 'scissors'){
if (computerChoice === 'rock'){
return 'you lost';
}else{
return 'you won';
}
}
}
console.log(determineWinner(userChoice, computerChoice));
It's simply because you are not passing the arguments to the determineWinner function.
Change the last line to:
determineWinner(userChoice,computerChoice);
Currently it's considering both choices as null and returning you the output as draw!