Mechanics of the Javascript rock, paper, scissors game -


okay has been bugging me , haven't been able find answer. here's codeacademy's rock, paper, scissors game:

var userchoice = prompt("do choose rock, paper or scissors?"); var computerchoice = math.random(); if (computerchoice < 0.34) {     computerchoice = "rock"; } else if(computerchoice <= 0.67) {     computerchoice = "paper"; } else {     computerchoice = "scissors"; } console.log("computer: " + computerchoice);  var compare = function(choice1, choice2){  if(choice1 === choice2) {     return "the result tie!"; }  else if(choice1 === "rock") {  if(choice2 === "scissors") {     return "rock wins"; } else {     return "paper wins";     } }  else if(choice1 === "paper") {     if(choice2 === "rock") {         return "paper wins"     } else {     return "scissors wins"     } }  else if(choice1 === "scissors") {     if(choice2 === "rock") {         return "rock wins" }     else {         return "scissors wins"     } } };  compare(userchoice, computerchoice); 

now wondering is, how computer know choice1 , choice2 when uses if/else loop determines winner? declared on userchoice , computerchoice functions , if how? links documentation helpful. trying understand how javascript works. thank you!

compare function takes choice1 , choice2 arguments. values determined when function called. example, if have 2 variables foo , bar containing strings this:

var foo = 'rock'; var bar = 'paper'; 

and call compare passing 2 variables, so:

compare(foo, bar); 

then, inside compare, choice1 'foo' , choice2 'bar'.

for strings, same calling compare this:

compare('rock', 'paper'); 

if @ rest of codecademy example see call compare further down.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -