Cool Spot
Cool Spot

Reputation: 33

Javascript if statement only returns true

I am writing a rock paper scissors game.

console.log for the player shows the selected value.

console.log for the computer shows the selected value.

The if statement to check if the values match always returns true, even if the console log shows them to be different.

I have tried writing it a few different ways but always end up with this issue.

// Player Choice Selection 
var player = function() {
  const playerSelects = "rock";
  return playerSelects;
};
console.log(player());

// Computer Choice Selection
var computer = function() {
  const computerSelects = ['rock', 'paper', 'scissors'];
  let randomSelection = Math.floor(Math.random() * 3);
  return computerSelects[randomSelection];
};
console.log(computer());

// Game 
var game = function(player, computer) {
  if (player == computer) {
    const draw = "its a draw";
    return draw;
  }
};
console.log(game());

Can anyone tell me where I am going wrong with this?

Thanks

Upvotes: 1

Views: 98

Answers (3)

adprocas
adprocas

Reputation: 1913

You aren't providing anything to the game() function.

I think this is what you would want.

As mentioned, player == computer isn't intuitive due to the naming as well. One wouldn't expect a player to ever be equal to a computer. It would be better to have something like playerValue == computerValue.

// Player Choice Selection 
var player = function() {
  const playerSelects = "rock";
  return playerSelects;
};

// Computer Choice Selection
var computer = function() {
  const computerSelects = ['rock', 'paper', 'scissors'];
  let randomSelection = Math.floor(Math.random() * 3);
  return computerSelects[randomSelection];
};

// Game 
var game = function(playerValue, computerValue) {
  if (playerValue === computerValue) {
    return "its a draw";
  } else {
    return "It's not a draw";
  }
};

var playerValue = player();
var computerValue = computer();

console.log(playerValue);
console.log(computerValue);
console.log(game(playerValue, computerValue));

If you don't have a need for the function expressions, it might be best to use regular functions, like this:

// Player Choice Selection 
function getPlayerChoice() {
  const playerSelects = "rock";
  return playerSelects;
};

// Computer Choice Selection
function getComputerChoice() {
  const computerSelects = ['rock', 'paper', 'scissors'];
  let randomSelection = Math.floor(Math.random() * 3);
  return computerSelects[randomSelection];
};

// Game 
function playGame(playerChoice, computerChoice) {
  if (playerChoice === computerChoice) {
    return "its a draw";
  } else {
    return "It's not a draw";
  }
};

var playerChoice = getPlayerChoice();
var computerChoice = getComputerChoice();

console.log(playerChoice);
console.log(computerChoice);
console.log(playGame(playerChoice, computerChoice));

Upvotes: 1

jonode
jonode

Reputation: 797

You've got a few things wrong. That some others have partially fixed, allow me to explain:

// Player Choice Selection 
var player = function() {
  const playerSelects = "rock";
  return playerSelects;
};
// Don't log the result here, because it will change in the game instance.

// Computer Choice Selection
var computer = function() {
  const computerSelects = ['rock', 'paper', 'scissors'];
  let randomSelection = Math.floor(Math.random() * 3);
  return computerSelects[randomSelection];
};
// Don't log the result here, because it will change in the game instance.

// Game
// Don't pass the player and computer functions in, because they're in global scope already.
var game = function() {
  // Call an instance of the player, and computer functions.
  // We do this so that each game we play, has new results.
  var playerResult = player();
  var computerResult = computer();
  // Log each result
  console.log(playerResult, computerResult)

  if (playerResult == computerResult) {
    const draw = "its a draw";
    return draw;
  }
};
console.log(game());

Upvotes: 0

Jonas Wilms
Jonas Wilms

Reputation: 138235

Game is a function taking two arguments:

 var game = function(player, computer){
    if(player == computer){

Therefore if you call it as

 game()

... player and computer will both be undefined as you pass no value in. Might do:

 game(computer(), player())

...however you should really dont shadow these variable names...

Upvotes: 1

Related Questions