My Function For Rock, Paper, Scissors Is Not Working - javascript

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>

Related

How to use function expression values in other functions?

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();

Rock Paper Scissors increasing label value on function result

I am currently trying to learn Javascript, and I am trying to make a simple Rock, Paper, Scissors application. At the moment my code looks like this
function computerPlay() {
let options = ['Rock', 'Paper', 'Scissors'];
return options[Math.floor(Math.random() * options.length)]
}
computerWin = 0;
userWin = 0;
const buttons = document.querySelectorAll('button');
buttons.forEach((button) => {
button.addEventListener('click', () => {
computerSelection = computerPlay();
playerSelection = button.id;
var userScore = document.getElementById('userScore');
var computerScore = document.getElementById('computerScore');
const result = playRound(playerSelection, computerSelection);
if (result == 1) {
userWin++;
userScore.nodeValue = Number(userWin);
} else if (result == 2) {
computerWin++;
computerScore.nodeValue = Number(computerWin);
}
});
});
function playRound(playerSelection, computerSelection) {
playerSelection = playerSelection.toLowerCase();
computerSelection = computerSelection.toLowerCase();
let playerWon = 1
let computerWon = 0
let tie = 2
if (playerSelection == computerSelection) {
return tie;
}
if (playerSelection === "rock") {
if (computerSelection === "scissors") {
return playerWon;
}
if (computerSelection === "paper") {
return computerWon;
}
}
if (playerSelection === "scissors") {
if (computerSelection === "paper") {
return playerWon;
}
if (computerSelection === "rock") {
return computerWon;
}
}
if (playerSelection === "paper") {
if (computerSelection === "rock") {
return playerWon;
} else {
return computerWon;
}
}
}
<button id="rock">ROCK</button>
<button id="paper">PAPER</button>
<button id="scissors">SCISSORS</button>
<br>
<p>Vlad : </p>
<input type="text" id="userScore" value="0" />
<p>Computer : </p>
<input type="text" id="computerScore" value="0" />
When I press any of the buttons, the round is played, a result is returned, but the value inside the labels never changes.
You should use .value and not .nodeValue
Rock Paper Scissor Video Tutorial
function computerPlay() {
let options = ['Rock', 'Paper', 'Scissors'];
return options[Math.floor(Math.random() * options.length)]
}
computerWin = 0;
userWin = 0;
const buttons = document.querySelectorAll('button');
buttons.forEach((button) => {
button.addEventListener('click', () => {
computerSelection = computerPlay();
playerSelection = button.id;
var userScore = document.getElementById('userScore');
var computerScore = document.getElementById('computerScore');
const result = playRound(playerSelection, computerSelection);
if (result == 1) {
userWin++;
userScore.value = Number(userWin);
} else if (result == 2) {
computerWin++;
computerScore.value = Number(computerWin);
}
});
});
function playRound(playerSelection, computerSelection) {
playerSelection = playerSelection.toLowerCase();
computerSelection = computerSelection.toLowerCase();
let playerWon = 1
let computerWon = 0
let tie = 2
if (playerSelection == computerSelection) {
return tie;
}
if (playerSelection === "rock") {
if (computerSelection === "scissors") {
return playerWon;
}
if (computerSelection === "paper") {
return computerWon;
}
}
if (playerSelection === "scissors") {
if (computerSelection === "paper") {
return playerWon;
}
if (computerSelection === "rock") {
return computerWon;
}
}
if (playerSelection === "paper") {
if (computerSelection === "rock") {
return playerWon;
} else {
return computerWon;
}
}
}
<button id="rock">ROCK</button>
<button id="paper">PAPER</button>
<button id="scissors">SCISSORS</button>
<br>
<p>Vlad : </p>
<input type="text" id="userScore" value="0" />
<p>Computer : </p>
<input type="text" id="computerScore" value="0" />
you need to set the value of the input. Also, your logic for result was a little messed up. Take a look at the following and let me know if this works for you.
function computerPlay() {
let options = ['Rock', 'Paper', 'Scissors'];
return options[Math.floor(Math.random() * options.length)]
}
computerWin = 0;
userWin = 0;
const buttons = document.querySelectorAll('button');
buttons.forEach((button) => {
button.addEventListener('click', () => {
computerSelection = computerPlay();
playerSelection = button.id;
var userScore = document.getElementById('userScore');
var computerScore = document.getElementById('computerScore');
const result = playRound(playerSelection, computerSelection);
if (result === 1) {
userWin++;
userScore.value = userWin;
} else if (result === 0) {
computerWin++;
computerScore.value= computerWin;
}else if(result === 2){
userWin= userWin + .5;
userScore.value = userWin;
computerWin=computerWin + .5;
computerScore.value= computerWin;
}
});
});
function playRound(playerSelection, computerSelection) {
playerSelection = playerSelection.toLowerCase();
computerSelection = computerSelection.toLowerCase();
let playerWon = 1
let computerWon = 0
let tie = 2
if (playerSelection == computerSelection) {
return tie;
}else if (playerSelection === "rock") {
if (computerSelection === "scissors") {
return playerWon;
}
if (computerSelection === "paper") {
return computerWon;
}
}else if (playerSelection === "scissors") {
if (computerSelection === "paper") {
return playerWon;
}
if (computerSelection === "rock") {
return computerWon;
}
}else if (playerSelection === "paper") {
if (computerSelection === "rock") {
return playerWon;
} else {
return computerWon;
}
}
}
<button id="rock">ROCK</button>
<button id="paper">PAPER</button>
<button id="scissors">SCISSORS</button>
<br>
<p>Vlad : </p>
<input type="text" id="userScore" value="0" />
<p>Computer : </p>
<input type="text" id="computerScore" value="0" />

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'));

Having trouble with Rock Paper Scissors Lizard Spock Final in javascript

So I am a student who is working on his final and I have been pulling examples from other programs we have already done. I can get everything to work but the results its a simple paper rock scissor lizard spock game but im not sure why the results are not working
<html>
<head>
<script type ="text/javascript">
var gameResults // Game Results
var playerChoice // Players choice
var BR = "<br />"; // Line break
var ES = ""; // Empty space
var PA = "<p />"; // full paragraph break
var NL = "\n"; // New Line
function winResults(string)
{
gameResults = wcType;
}
function setChoice(pcType)
{
playerChoice = pcType;
}
function displayResults()
{
var name = document.RockPaperSpockForm.name.value;
var computerChoice = Math.random();
if (computerChoice < 0.2)
{
computerChoice = "Rock";
}
else if (computerChoice <= 0.4)
{
computerChoice = "Paper";
}
else if (computerChoice <= 0.6)
{
computerChoice = "Scissors";
}
else if (computerChoice <= 0.8)
{
computerChoice = "Lizard";
}
else
{
computerChoice = "Spock";
}
var compare = function(playerChoice, computerChoice)
{
if (playerChoice === computerChoice)
{
winResults(Tie);
}
else if (playerChoice === "Rock")
{
if (computerChoice === "Scissors")
{
winResults(Win);
}
else if (computerChoice === "Paper")
{
winResults(Lose);
}
else if (computerChoice === "Lizard")
{
winResults(Win);
}
else
{
winResults(Lose);
}
}
else if (playerChoice === "Paper")
{
if (computerChoice === "Scissors")
{
winResults(Lose);
}
else if (computerChoice === "Rock")
{
winResults(Win);
}
else if (computerChoice === "Lizard")
{
winResults(Lose);
}
else
{
winResults(Win);
}
}
else if (playerChoice === "Scissors")
{
if (computerChoice === "Paper")
{
winResults(Win);
}
else if (computerChoice === "Rock")
{
winResults(Lose);
}
else if (computerChoice === "Lizard")
{
winResults(Win);
}
else
{
winResults(Lose);
}
}
else if (playerChoice === "Lizard")
{
if (computerChoice === "Scissors")
{
winResults(Lose);
}
else if (computerChoice === "Rock")
{
winResults(Lose);
}
else if (computerChoice === "Paper")
{
winResults(Win);
}
else
{
winResults(Win);
}
}
else if (playerChoice === "Spock")
{
if (computerChoice === "Scissors")
{
winResults(Win);
}
else if (computerChoice === "Rock")
{
winResults(Win);
}
else if (computerChoice === "Lizard")
{
winResults(Lose);
}
else
{
winResults(Lose);
}
}
}
compare(playerChoice, computerChoice);
alert("Hello! " + name + " you have chosen " + playerChoice + " and the computer has chosen " + computerChoice + "!" + NL + "You " + gameResults + "!");
}
</script>
</head>
<body bgcolor="Azure">
<h3>Rock Paper Scissors Lizard Spock!</h3>
<form name="RockPaperSpockForm" action="">
<strong>Enter your name:</strong><br />
<input type="text" name="name" value="Name" size="40"><p />
<strong>Select Paper, Rock, Scissors, Lizard, or Spock:</strong><br />
<input type="radio" name="choice" value="Paper" onclick="setChoice(this.value)" /><img src="PaperThumb.JPG"><p />
<input type="radio" name="choice" value="Rock" onclick="setChoice(this.value)" /><img src="RockThumb.JPG"><p />
<input type="radio" name="choice" value="Scissors" onclick="setChoice(this.value)" /><img src="ScissorsThumb.JPG"><p />
<input type="radio" name="choice" value="Lizard" onclick="setChoice(this.value)" /><img src="LizardThumb.JPG"><p />
<input type="radio" name="choice" value="Spock" onclick="setChoice(this.value)" /><img src="SpockThumb.JPG"><p />
<input type="button" name="displaybutton" value="Go" onclick="displayResults()" /><p />
<textarea name="messageBox" readonly="true" value="" rows="8" cols="50"></textarea><br />
</form>
</body>
</html>
what i was going for is the results would set the variable inside the function to Tie, Win, Lose and then i could just attach it to the alert but its not working showing up as undefined. Any help would be appreciated I am stuck.
If you change this line of code:
function winResults(string)
to read:
function winResults(wcType)
You will also need to fix your calls to winResults so that the parameter that you pass is a string literal in each case -- right now all of these calls are written as though they pass a variable named WIN, LOSE or TIE. For example, where you currently have:
winResults(TIE)
you should change that to:
winResults("TIE")

Exiting out of a function in Javascript. Simple rock, paper, scissors game

How do i stop the function from executing if the userChoice is null or a word apart from rock,paper or scissors.
I've tried to use return but i couldn't get it to work.
Any help is appreciated.
Thanks
JS Fiddle Link = http://jsfiddle.net/Renay/d9bea2ra/1/
var userChoice = prompt('Do you choose rock, paper or scissors?');
var compChoice = Math.random();
if (compChoice <= 0.34) {
compChoice = 'rock';
} else if (compChoice <= 0.67) {
compChoice = 'paper';
} else {
compChoice = 'scissors';
}
function compare() {
if (userChoice == compChoice) {
console.log( 'The result is a tie!');
} else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper') ) {
console.log( 'You win!');
} else {
console.log('You lose!');
}
if (userChoice === null) {
console.log('Please select an option');
} else if (userChoice !== 'rock'&&'paper'&&'scissors') {
console.log('Please select rock, paper or scissors');
}
}
console.log('Your choice = ' + userChoice);
console.log('Computer Choice = ' + compChoice);
compare();
The condition in your if statement is wrong. It should be:
if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors')
An expression of the form e1 && e2 && e3 && ... evaluates to last eN sub-expression if all of them truthy. So your test was equivalent to:
if (userChoice !== 'scissors')
You should put that check before displaying the result of the game, and return from the function then. So it should be:
function compare() {
if (userChoice === null) {
console.log('Please select an option');
return;
} else if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') {
console.log('Please select rock, paper or scissors');
return;
}
if (userChoice == compChoice) {
console.log( 'The result is a tie!');
} else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper') ) {
console.log( 'You win!');
} else {
console.log('You lose!');
}
}
Just do return;
function compare() {
if(!userChoice)
return;
// (...) more code
}
A return call exits the function. But note that you can only use return inside a function
If i understand correctly, you want to return to the beginning of the code. You should wrap the entire thing in a function, then call that function again:
function rockPaperScissors() {
var userChoice = prompt('Do you choose rock, paper or scissors?');
var compChoice = Math.random();
if (compChoice <= 0.34) {
compChoice = 'rock';
} else if (compChoice <= 0.67) {
compChoice = 'paper';
} else {
compChoice = 'scissors';
}
function compare() {
if (userChoice == compChoice) {
console.log( 'The result is a tie!');
} else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper') ) {
console.log( 'You win!');
} else {
console.log('You lose!');
}
if (userChoice === null) {
console.log('Please select an option');
rockPaperScissors();
} else if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') { // fixed
console.log('Please select rock, paper or scissors');
rockPaperScissors();
}
}
console.log('Your choice = ' + userChoice);
console.log('Computer Choice = ' + compChoice);
compare();
}
rockPaperScissors();
You also had an error with checking if it isn't one of rock, paper, or scissors, so i fixed that. (It seems Barmar got to it before me.)
I updated each if/else part of your function to exit when it match if/else condition with return false. In addition I changed the if (userChoice === null) check to if (userChoice === "") because it looks like that prompt('Do you choose rock, paper or scissors?'); returns an empty string when the user doesn't enter any value. Fiddle Also, I just updated the beginning of the function to work accordingly to **Barmar'**s suggestion (he got it right first time around).
function compare() {
if (userChoice === "") {
console.log('Please select an option');
return false;
} else if ((userChoice !== 'rock') && (userChoice !=='paper') && (userChoice !=='scissors')) {
console.log('Please select rock, paper or scissors');
return false;
}
if (userChoice == compChoice) {
console.log('The result is a tie!');
return false;
} else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper')) {
console.log('You win!');
return false;
} else {
console.log('You lose!');
return false;
}
}

Categories