javascript - How to test for undefined variable repeatedly? -


i trying make beginner project, rock, paper, scissors game. having trouble variable, made 3 buttons, rock, paper , scissors, , when click 1 of buttons return variable function. when assign global variable value of function, first says undefined , when click again says rock, papers, or scissors, tried if statement alerts when variable not null doesn't seem work.

js code

function gamefunction(choice) {    userchoice = choice;    return userchoice;  }    playerchoice = gamefunction();    if (playerchoice != null) {    alert("test");  }

that's because variable playerchoice undefined first time load code. need instead (if decide insist in global variables) define playerchoice defined , valid value.

like:

playerchoice = "rock" 

this happens because javascript follows (among others) event-oriented programming paradigm, in first iteration of application, assume waits user choose click event?, value have before click event?

in short, need define global variable known state, if player change later.

hope helps.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -