How to fix undefined variable in javascript

Chris King

For some reason my function is not returning a 1 or 2 even though it's specifically setup to do so. What am I doing wrong? I'm looking at the chrome dev tools and it's telling me that var processed is undefined.

I'm quite stumped on this. I've been reading if it's because a variable could be used as a parameter but I'm not sure if this is the case

var processChoices = function (player, computer){
    switch (player) {
        case player == 'rock':
            if (computer == 'paper'){
                var winner = 2; 
            } else if (computer == 'scissors'){
                var winner = 1; 
            }
            break;
        case player == 'paper':
            if (computer == 'scissors'){
                var winner = 2;
            } else if (computer == 'rock'){
                var winner = 1;
            }
            break;
        case player == 'scissors':
            if (computer == 'rock'){
                var winner = 2;
            } else if (computer == 'paper'){
                var winner = 1;
            }
            break;
        default:
            if (computer == player){
        var winner = console.log('We have a tie, go again!');
            } 
            break;
    }
    return winner
}

var determineWinner = function (){
    var computer = computerPlay();
    var player = playerChoice();
    var processed = processChoices(player, computer);

    if (processed == 1){
        playerCount += 1;
    } else {
        computerCount += 1;
    }
    var message = (processed == 2) ? `The computer wins! ${computer} beats ${player}!` : `The player wins! ${player} beats ${computer}!`;
    console.log(message);

}

I'm expecting the output of var processed to be 1 or 2. It's coming back as undefined.

lucasvw

It looks like you're not using the switch statement correctly. Your case statements need to just be the value that you want to match. See below.

It's would also be good to declare the variable winner once.

var processChoices = function(player, computer) {
  var winner = 0;
  switch (player) {
    case 'rock':
      if (computer == 'paper') {
        winner = 2;
      } else if (computer == 'scissors') {
        winner = 1;
      }
      break;
    case 'paper':
      if (computer == 'scissors') {
        winner = 2;
      } else if (computer == 'rock') {
        winner = 1;
      }
      break;
    case 'scissors':
      if (computer == 'rock') {
        winner = 2;
      } else if (computer == 'paper') {
        winner = 1;
      }
      break;
    default:
      if (computer == player) {
        console.log('We have a tie, go again!');
      }
      break;
  }
  return winner
}

var computer = "rock";
var player = "paper";
console.log("Player chose:", player, "Computer chose:", computer);
console.log("The winner is...", processChoices(player, computer));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to check if a variable is both null and /or undefined in JavaScript

How to check if a JavaScript variable is NOT undefined?

How to check for an undefined or null variable in JavaScript?

How do I fix PyDev "Undefined variable from import" errors?

How to check for both undefined and null variable in JavaScript?

How to check if a variable is undefined versus it is undeclared in javascript?

how to fix "Undefined variable: subtask" in Laravel 5.2

How to fix Undefined variable: image in Laravel 5.6

How to fix: "undefined response from fetch" Javascript

How do I fix an undefined variable in my program?

How to fix "TypeError: Cannot read property 'map' of undefined" in JavaScript?

How to fix 'TypeError: Cannot read property 'title' of undefined' in Javascript

How to fix that when variable is compared it becomes undefined in JavaScript?

How to fix the error undefined variable "$labels"in Prometheus?

How to fix 'Typerror is undefined' message for a function in Javascript?

how to Fix undefined variable?

PHP error undefined variable notice how to fix?

How to check undefined variable javascript?

How do I fix "MANPATH: undefined variable." in csh?

How to fix "Undefined variable: collaborators"?

How do I fix it 'error: undefined' in javascript?

how fix this error "Undefined variable: products (0)"

How to fix "Undefined Variable" in node-sass?

How to fix frontend javascript in handlebars catching undefined value from server?

How to fix TypeError: Cannot read property 'length' of undefined in JavaScript?

How I can skip variable if undefined in javascript

How to make context variable and fix error that 'Undefined name 'context'.'

How to fix error "undefined variable:sin" in plotting in gnuplot?

how do i fix "undefined variable"

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive