+ 7
why is the output of this code " 'rockfunc' is undefined " please explain ;-) [SOLVED]
https://code.sololearn.com/W3Z7Z31b53sz/?ref=app [it really works] The HTML and JavaScript coding is finished just the CSS is remaining, I will finish it and will present it before you guys thx for the help
8 Answers
+ 6
This works
var game = ["Rock", "Paper", "Scissors"];
function randomizer() {
var random = game[Math.floor(Math.random() * game.length)];
return random;
}
var rock = game[0];
var paper = game[1];
var scissors = game[2];
function rockfunc() {
var random = randomizer();
if(random == rock) {
alert("computer chose rock its a draw");
} else if(random == paper) {
alert("computer chose paper you lost");
}else{
alert("computer chose scissors you won");
}
}
+ 12
Another version, replaced nested if/else by switch/case and cleaned up a little:
var game = ["Rock", "Paper", "Scissors"];
var rock = game[0];
var paper = game[1];
var scissors = game[2];
var random;
function randomizer() {
random = game[Math.floor(Math.random() *
game.length)];
return random;
}
function rockfunc() {
randomizer();
switch(random) {
case rock:
alert("computer chose rock its a draw");
break;
case paper:
alert("computer chose paper you lost");
break;
case scissors:
alert("computer chose scissors you won");
break;
}
}
+ 9
@The Coding Sloth
Why a default case for the switch? There are only 3 possible cases here, so in this context you don't really need a default case.
+ 9
Its done I have finished this project the to u guys !
https://code.sololearn.com/W3Z7Z31b53sz/?ref=app
+ 7
if you use a switch statement I would suggest a default at the end
+ 7
I think switch and break condition would be the best here
+ 7
Well I used your method first The Coding Sloth and it worked, and then I realized it was just a single line of code I had forgot to write that is
"var random = randomizer ();
+ 6
@Tashi N o oops, I didn't really read it that well I guess.