+ 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

6th May 2017, 6:10 PM
_Retr0/-
_Retr0/- - avatar
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"); } }
6th May 2017, 6:19 PM
The Coding Sloth
The Coding Sloth - avatar
+ 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; } }
6th May 2017, 6:33 PM
Tashi N
Tashi N - avatar
+ 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.
6th May 2017, 6:42 PM
Tashi N
Tashi N - avatar
+ 9
Its done I have finished this project the to u guys ! https://code.sololearn.com/W3Z7Z31b53sz/?ref=app
7th May 2017, 12:19 PM
_Retr0/-
_Retr0/- - avatar
+ 7
if you use a switch statement I would suggest a default at the end
6th May 2017, 6:35 PM
The Coding Sloth
The Coding Sloth - avatar
+ 7
I think switch and break condition would be the best here
6th May 2017, 6:44 PM
_Retr0/-
_Retr0/- - avatar
+ 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 ();
7th May 2017, 6:00 AM
_Retr0/-
_Retr0/- - avatar
+ 6
@Tashi N o oops, I didn't really read it that well I guess.
6th May 2017, 7:29 PM
The Coding Sloth
The Coding Sloth - avatar