How to use function expression values in other functions? - javascript

I'm learning Javascript and I make a rock, scissors, paper project. I already make it work with the sample code but now I want to use a function expression to use its value in other functions. When I load the code it gave me undefined and I don't know why. I assign all the values.
Here is my code.
const userChoice = userInput => {
//userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
return userInput;
} else {
console.log('Error!');
}
}
//console.log(getUserChoice('Fork'));
const computerChoice = function() {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors'
}
};
//console.log(getComputerChoice());
function determineWinner(userChoice, computerChoice) {
console.log()
if (userChoice === computerChoice) {
return 'The game is a tie!'
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'paper') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'bomb') {
return 'You won!';
}
}
const playGame = () => {
console.log('You threw: ' + userChoice('rock'));
console.log('The computer threw: ' + computerChoice());
console.log(determineWinner(userChoice, computerChoice));
}
playGame();
Please help me.

You're passing the function userChoice as a param, you should store the result of calling the function userChoice and pass the result as a param in the function determineWinner
const userChoice = userInput => {
//userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
return userInput;
} else {
console.log('Error!');
}
}
//console.log(getUserChoice('Fork'));
const computerChoice = function() {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors'
}
};
//console.log(getComputerChoice());
function determineWinner(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return 'The game is a tie!'
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'paper') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'bomb') {
return 'You won!';
}
}
const playGame = () => {
let uc = userChoice('rock');
console.log('You threw: ' + uc);
console.log('The computer threw: ' + computerChoice());
console.log(determineWinner(uc, computerChoice));
}
playGame();

You should assign the outputs of userChoice and computerChoice to variables before passing them to determineWinner
const userChoice = userInput => {
//userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
return userInput;
} else {
console.log('Error!');
}
}
//console.log(getUserChoice('Fork'));
const computerChoice = function() {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors'
}
};
//console.log(getComputerChoice());
function determineWinner(userChoice, computerChoice) {
console.log()
if (userChoice === computerChoice) {
return 'The game is a tie!'
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'paper') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'bomb') {
return 'You won!';
}
}
const playGame = () => {
const userChose = userChoice('rock')
const computerChose = computerChoice()
console.log('You threw: ' + userChose);
console.log('The computer threw: ' + computerChose);
console.log(determineWinner(userChose, computerChose));
}
playGame();

Try This
const userChoice = userInput => {
//userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
return userInput;
} else {
console.log('Error!');
}
}
//console.log(getUserChoice('Fork'));
const computerChoice = function() {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors'
}
};
//console.log(getComputerChoice());
function determineWinner(userChoice, computerChoice) {
console.log()
if (userChoice === computerChoice) {
return 'The game is a tie!'
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'paper') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'bomb') {
return 'You won!';
}
}
const playGame = () => {
console.log('You threw: ' + userChoice('rock'));
console.log('The computer threw: ' + computerChoice());
console.log(determineWinner(userChoice('rock'), computerChoice()));
}
playGame();

just a few mistakes.
You need to store the values into variables.
const userChoice = userInput => {
//userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
return userInput;
} else {
console.log('Error!');
}
}
//console.log(getUserChoice('Fork'));
const computerChoice = function() {
const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors'
}
};
//console.log(getComputerChoice());
function determineWinner(userChoice, computerChoice) {
console.log()
if (userChoice === computerChoice) {
return 'The game is a tie!'
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'paper') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!'
}
}
if (userChoice === 'bomb') {
return 'You won!';
}
}
const playGame = () => {
const threw = {
computer: null,
user: null
}
threw.user = userChoice("rock")
threw.computer = computerChoice()
console.log('You threw: ' + threw.user);
console.log('The computer threw: ' + threw.computer);
console.log(determineWinner(userChoice(threw.user), threw.computer));
}
playGame();

Related

Getting tie always with Rock Paper Scissors game Javascript

const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput
} else {
console.log('Error!')
}
}
const getcomputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3)
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'Tie'
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'computer won'
} else {
return 'user won'
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'computer won'
} else {
return 'user won'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'computer won'
} else {
return 'user won'
}
}
}
const playGame = () => {
var userChoice = getUserChoice()
console.log(userChoice)
var computerChoice = getcomputerChoice()
console.log(computerChoice)
console.log(determineWinner(userChoice, computerChoice))
}
console.log(determineWinner())
So I wanted to do a project with JS, was following codeacadem's course on the subject. But for some reason I am always getting a tie which I do not understand why. Can anyone please help me? I thought this is the correct code and every single object was working properly.
UPDATE:
After giving a parameter to the userchoice (getUserChoice('scissors')) I got this error: userInput = userInput.toLowerCase();
TypeError: Cannot read properties of undefined (reading 'toLowerCase')
Initializing playgame didnt solve the issue
I've corrected your code with a few things:
As #James has commented, getUserChoice() expects a parameter for the choice of the user but none is given at playGame().
As #Alon Eitan has commented, console.log(determineWinner()) doesn't play the game at all as it just calls determineWinner() with the two parameters undefined. And actually that's why Tie is always returned as undefined == undefined.
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput
} else {
console.log('Error!')
}
}
const getcomputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3)
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'Tie'
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'computer won'
} else {
return 'user won'
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'computer won'
} else {
return 'user won'
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'computer won'
} else {
return 'user won'
}
}
}
const playGame = () => {
var userChoice = getUserChoice("rock")
console.log(userChoice)
var computerChoice = getcomputerChoice()
console.log(computerChoice)
console.log(determineWinner(userChoice, computerChoice))
}
playGame()

The variables inside the functions can't be called

I'm doing this rock, paper, and scissors game, however, my last function it's not working properly, it won't execute that block of code, and what it's wrong with the variables in the last function, they cannot be called, I've tried to change the parameters, I also debugged the code and to the extent I notice just the last function it's not working properly
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput == "rock" || userInput == "paper" || userInput == "scissors") {
return userInput;
} else {
console.log('Enter a valid option')
};
};
function getComputerChoice(any) {
return Math.floor(Math.random() * any)
}
switch (getComputerChoice(3)) //userInput
{
case 0:
console.log('rock');
break;
case 1:
console.log('paper');
break;
case 2:
console.log('scissors');
break;
default:
console.log('Select a valid option')
}
function determineWinner(getUserChoice, getComputerChoice) {
if (getUserChoice === getComputerChoice) {
return 'The game was a tie'
}
if (getUserChoice === 'rock') {
if (getComputerChoice === 'paper') {
return 'The computer won!'
} else {
return 'You won'
}
}
if (getUserChoice === 'scissors') {
if (getComputerChoice === 'rock') {
return 'Computer won!'
} else {
return 'You won!'
}
}
}
const playGam = () => {
const userChoice = getUserChoice('scissors');
const computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
playGam();
As posted, getComputerChoice just does random math and multiplies by a (sometimes missing) param. Consider changing to return a random choice from the set of ['rock', 'paper', 'scissors']...
function getComputerChoice() {
const choices = ['rock', 'paper', 'scissors'];
return choices[Math.floor(Math.random() * choices.length)]
}
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput == "rock" || userInput == "paper" || userInput == "scissors") {
return userInput;
} else {
console.log('Enter a valid option')
};
};
function getComputerChoice() {
const choices = ['rock', 'paper', 'scissors'];
return choices[Math.floor(Math.random() * choices.length)]
}
switch (getComputerChoice(3)) //userInput
{
case 0:
console.log('rock');
break;
case 1:
console.log('paper');
break;
case 2:
console.log('scissors');
break;
default:
console.log('Select a valid option')
}
function determineWinner(getUserChoice, getComputerChoice) {
if (getUserChoice === getComputerChoice) {
return 'The game was a tie'
}
if (getUserChoice === 'rock') {
if (getComputerChoice === 'paper') {
return 'The computer won!'
} else {
return 'You won'
}
}
if (getUserChoice === 'scissors') {
if (getComputerChoice === 'rock') {
return 'Computer won!'
} else {
return 'You won!'
}
}
}
const playGam = () => {
const userChoice = getUserChoice('scissors');
const computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
playGam();
We could also improve the code in a few other ways...
// make this global
const choices = ['rock', 'paper', 'scissors'];
const beats = { rock: 'scissors', scissors: 'paper', paper: 'rock' };
// general purpose fn to validate
const validateChoice = choice => {
return choices.includes(choice.toLowerCase());
};
// a tightly specified thing to determine winner
const determineWinner(userChoice, computerChoice) {
if (computerChoice === userChoice) return 'tie';
return beats[userChoice] === computerChoice ? 'user win' : 'computer win';
}
You're not setting a variable value with your swtich statement. You need to return what's the computer's choice, so I've put your switch statement inside the getComputerChoice function and then it returns the output and you can use it in your playGam function. I've updated that as well to display the winner in every outcome.
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput == "rock" || userInput == "paper" || userInput == "scissors") {
return userInput;
} else {
console.log('Enter a valid option')
};
};
function getComputerChoice(any) {
const randomNo = Math.floor(Math.random() * any)
switch (randomNo) //userInput
{
case 0:
return 'scissors';
break;
case 1:
return 'paper';
break;
case 2:
return 'rock';
break;
default:
return 'Select a valid option';
}
}
function determineWinner(getUserChoice, getComputerChoice) {
if (getUserChoice === getComputerChoice) {
return 'The game was a tie'
}
switch (getUserChoice) {
case 'paper':
return getComputerChoice === 'scissors' ? 'The computer won!' : 'You won';
case 'rock' :
return getComputerChoice === 'scissors' ? 'The computer won!' : 'You won';
case 'scissors' :
return getComputerChoice === 'paper' ? 'You won' : 'The computer won!';
default:
return 'Something went wrong'
}
}
const playGam = () => {
const userChoice = getUserChoice('paper');
const computerChoice = getComputerChoice(3);
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
playGam();

Showing the same result in Rock Paper Scissors - Java Script

**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!

Declaration or statement expected in If Statements

So I am working on some code to build a simple application in Javascript where the computer has a random number assigned to rock, paper, scissors and choose randomly and then has a userchoice as well. The code is not running not sure why.
I have tried adding semicolon to the end of the if statements that are inside the main if statement in the determineWinner function.
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
return userInput;
}else {
console.log('Error, you must type rock, paper, or scissors');
}
}
function getComputerChoice(){
var randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
function determineWinner(userChoice, computerChoice){
if(userChoice === computerChoice){
return 'This game is a tie!'
} else if(userChoice === 'rock'){
if(computerChoice === 'paper'){
return 'The computer has won!';
}else{
return 'The user has won';
}
} else if(userChoice === 'paper'){
if(computerChoice === 'scissors'){
return 'The computer has won!';
}else(computerChoice === 'rock');{
return 'The user has won!';
}
}else(userChoice === 'scissors');{
if(computerChoice === 'rock'){
return 'The computer has won!';
}else(computerChoice === 'paper');{
return 'The user has won!';
}
}
}
}
console.log(determineWinner('paper', 'scissors'));
When running the console.log at the end of the script it should display that the computer has won.
Since all of your if cases simply return, you don't need to use else since the if-case will always return. Like this:
function determineWinner(userChoice, computerChoice){
if (userChoice === computerChoice) {
return 'This game is a tie!'
}
if (userChoice === 'rock') {
if(computerChoice === 'paper') {
return 'The computer has won!';
}
return 'The user has won';
}
if (userChoice === 'paper') {
if(computerChoice === 'scissors'){
return 'The computer has won!';
}
return 'The user has won!';
}
if (userChoice === 'scissors') {
if(computerChoice === 'rock') {
return 'The computer has won!';
}
return 'The user has won!';
}
}
Another way is to just return at the end of the function with the answer like this:
function determineWinner(userChoice, computerChoice){
const results = [
'This game is a tie!',
'The computer has won!',
'The user has won!'
];
let winner;
if (userChoice === computerChoice) winner = 0;
else if (userChoice === 'rock') {
winner = computerChoice === 'paper' ? 1 : 2;
}
else if (userChoice === 'paper') {
winner = computerChoice === 'scissors' ? 1 : 2;
}
else if (userChoice === 'scissors') {
winner = computerChoice === 'rock' ? 1 : 2;
}
return results[winner];
}
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
return userInput;
}else {
console.log('Error, you must type rock, paper, or scissors');
}
}
function getComputerChoice(){
var randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
function determineWinner(userChoice, computerChoice){
if(userChoice === computerChoice){
return 'This game is a tie!'
} else if(userChoice === 'rock'){
if(computerChoice === 'paper'){
return 'The computer has won!';
}else if(computerChoice === 'scissors'){
return 'The user has won';
}
} else if(userChoice === 'paper'){
if(computerChoice === 'scissors'){
return 'The computer has won!';
}else if(computerChoice === 'rock'){
return 'The user has won!';
}
}else if(userChoice === 'scissors'){
if(computerChoice === 'rock'){
return 'The computer has won!';
}else if(computerChoice === 'paper'){
return 'The user has won!';
}
}
}
console.log(determineWinner('paper', 'scissors'));

My Function For Rock, Paper, Scissors Is Not Working

I have a function that is suppose to determine the winner of game. For some reason, it will always print the first if statement, even if it is not correct
I have tried making them all if statements, changing the brackets, and I split all the else if statements up instead of having only 3 and nothing is working.
function getElem(id) {
return document.getElementById(id);
}
var rounds;
function startGame() {
rounds = getElem("ROUNDS_TO_PLAY");
rounds = parseInt(rounds.value);
document.roundsRemaining = rounds;
getElem("ROUNDS_REMAINING").value = rounds;
}
/* Responds to user choice button click. */
function userChoice(userButton) {
updateStats();
displayUserChoice(userButton);
var compChoice = getComputerChoice();
getElem("COMPUTER_CHOICE_OUTPUT").value = compChoice;
displayComputerChoice(compChoice);
}
function updateStats() {
rounds=document.roundsRemaining;
rounds--;
document.roundsRemaining = rounds;
getElem("ROUNDS_REMAINING").value = document.roundsRemaining;
if (document.roundsRemaining < 1) {
alert("Game over");
getElem("ROCK_CHOICE").disabled=true;
//alert(getWinner(userButton, compChoice));
}
//alert(getWinner());
alert (determineWinner());
}
function getComputerChoice() {
var r = Math.floor(Math.random() * 3)
switch (r) {
case 0: return "ROCK";
case 1: return "PAPER";
case 2: return "SCISSORS";
default: console.log(r + " is not a valid computer choice.");
}
}
function displayUserChoice(userButton) {
var uco = getElem("USER_CHOICE_OUTPUT");
if (userButton == "ROCK") {
uco.value = "ROCK";
} else if (userButton == "PAPER") {
uco.value = "PAPER";
} else if (userButton == "SCISSORS") {
uco.value = "SCISSORS";
} else {
conosole.log(userButton + " is invalid!!");
}
}
function determineWinner(userButton,compChoice) {
if (userButton === compChoice) {
return 'It\'s a tie!';
}
else if (userButton === "ROCK" && compChoice === "PAPER") {
return 'Computer wins!';
}
else if (userButton === 'PAPER' && compChoice === 'SCISSORS') {
return 'Computer wins!';
}
else if (userButton === 'SCISSORS' && compChoice === 'ROCK') {
return 'Computer wins!';
}
else if (userButton === 'PAPER' && compChoice === 'ROCK')
{
return 'You win!';
}
else if (userButton === 'SCISSORS' && compChoice === 'PAPER') {
return 'You win!';
}
else {
return 'You win!';
}
}
//Html seperate file
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta charset="UTF-8">
<title>my game</title>
<script src="rsp.js" type="text/javascript"></script>
</head>
<body>
Rounds to Play: <input id="ROUNDS_TO_PLAY"><br>
<input type="button" id="START_GAME" value="Start!" onclick="startGame()">
Rounds Remaining: <input id="ROUNDS_REMAINING"><br><br><br>
<input type="button" id="ROCK_CHOICE" value="Rock" onclick="userChoice('ROCK')">
<input type="button" id="PAPER_CHOICE" value="Paper" onclick="userChoice('PAPER')">
<input type="button" id="SCISSORS_CHOICE" value="Scissors" onclick="userChoice('SCISSORS')">
<br>
User Chose: <input id="USER_CHOICE_OUTPUT" type="text" disabled="true"><br>
Computer Chose: <input id="COMPUTER_CHOICE_OUTPUT" type="text" disabled="true">
</body></html>
The output is suppose to show either computer wins, you win, or its a tie. It shows its a tie every single time.
function getElem(id) {
return document.getElementById(id);
}
function userChoice(userButton) {
displayUserChoice(userButton);
var compChoice = getComputerChoice();
getElem("COMPUTER_CHOICE_OUTPUT").value = compChoice;
updateStats();
}
function updateStats() {
console.log(determineWinner( (document.getElementById("USER_CHOICE_OUTPUT").value), (document.getElementById("COMPUTER_CHOICE_OUTPUT").value)));
}
function getComputerChoice() {
var r = Math.floor(Math.random() * 3)
switch (r) {
case 0: return "ROCK";
case 1: return "PAPER";
case 2: return "SCISSORS";
default: console.log(r + " is not a valid computer choice.");
}
}
function displayUserChoice(userButton) {
var uco = getElem("USER_CHOICE_OUTPUT");
if (userButton == "ROCK") {
uco.value = "ROCK";
} else if (userButton == "PAPER") {
uco.value = "PAPER";
} else if (userButton == "SCISSORS") {
uco.value = "SCISSORS";
} else {
conosole.log(userButton + " is invalid!!");
}
}
function determineWinner(userButton,compChoice) {
if (userButton === compChoice) {
return 'It\'s a tie!';
}
else if (userButton === "ROCK" && compChoice === "PAPER") {
return 'Computer wins!';
}
else if (userButton === 'PAPER' && compChoice === 'SCISSORS') {
return 'Computer wins!';
}
else if (userButton === 'SCISSORS' && compChoice === 'ROCK') {
return 'Computer wins!';
}
else if (userButton === 'PAPER' && compChoice === 'ROCK')
{
return 'You win!';
}
else if (userButton === 'SCISSORS' && compChoice === 'PAPER') {
return 'You win!';
}
else {
return 'You win!';
}
}
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta charset="UTF-8">
<title>my game</title>
</head>
<body>
<input type="button" id="ROCK_CHOICE" value="Rock" onclick="userChoice('ROCK')">
<input type="button" id="PAPER_CHOICE" value="Paper" onclick="userChoice('PAPER')">
<input type="button" id="SCISSORS_CHOICE" value="Scissors" onclick="userChoice('SCISSORS')">
<br>
User Choice: <input id="USER_CHOICE_OUTPUT" type="text" disabled="true"><br>
Computer Chose: <input id="COMPUTER_CHOICE_OUTPUT" type="text" disabled="true">
</body></html>

Categories