javascript - Alerts not opening from if statements -
i making rock paper scissors game, , want alerts determine picked , won, alerts dont pop up. found multiple errors going , fixed them, double checked everything, , gave triple check, alerts still not pop up?
this 1 of 3 functions (they similar):
function rock() { var computerchoice = math.random(); if (computerchoice < 0.34) { computerchoice = "rock"; } else if (computerchoice < 0.67) { computerchoice = "paper"; } else { computerchoice = "scissors"; } } if (computerchoice === "rock") { alert("link , computer both chose rock! it's tie!"); } else if (computerchoice === "scissors") { alert("link chose rock , computer chose scissors! computer took heart of damage!"); } else { alert("link chose rock , computer chose paper! link took heart of damage!"); } }
i think problem on line indicated:
function rock() { var computerchoice = math.random(); if (computerchoice < 0.34) { computerchoice = "rock"; } else if (computerchoice < 0.67) { computerchoice = "paper"; } else { computerchoice = "scissors"; } } // <-- don't think want closing brace here if (computerchoice === "rock") { alert("link , computer both chose rock! it's tie!"); } else if (computerchoice === "scissors") { alert("link chose rock , computer chose scissors! computer took heart of damage!"); } else { alert("link chose rock , computer chose paper! link took heart of damage!"); } }
the closing brace i've indicated brings end rock()
function. stands, rock()
function doesn't anything; assigns value "rock"
, "paper"
or "scissors"
@ random local variable, function ends without variable being used, value lost.
the rest of code run once when javascript first loaded browser. @ point, computerchoice
undefined
, , hence javascript error.
if remove errant brace, , reformat code, following:
function rock() { var computerchoice = math.random(); if (computerchoice < 0.34) { computerchoice = "rock"; } else if (computerchoice < 0.67) { computerchoice = "paper"; } else { computerchoice = "scissors"; } if (computerchoice === "rock") { alert("link , computer both chose rock! it's tie!"); } else if (computerchoice === "scissors") { alert("link chose rock , computer chose scissors! computer took heart of damage!"); } else { alert("link chose rock , computer chose paper! link took heart of damage!"); } }
i ran function several times. each time alerted 1 of 3 messages @ random.
Comments
Post a Comment