-1

I'm working on an a rock, paper, scissors game in Javascript, I've tried googling other examples of similar rock, paper, scissors games and they all pop up the same code. (Using Math.random) The instructions for my game are a little bit more precise.

 var choice = prompt("Please enter rock, paper, or scissors");
var min=1,max=3;
var x=Math.floor((Math.random() * 3) + 1);
if(x==1)
    chosen='Rock';
if(x==2)
   chosen='Paper';
if(x==3)
    chosen='Scissor';
document.write("The Computer chose "+chosen);

document.write("<p>You chose "+choice);

How can I compare what the computer has entered with what the user has entered since the user isn't entering a numerical value?

Coodaye
  • 21
  • 3
  • 1
    Welcome to SO. Questions without code will generally be voted down and closed. Please visit the [help] for more information on how SO works. Then search So for random and Array. – mplungjan Jun 17 '15 at 19:58
  • Seems a nice homework – AshBringer Jun 17 '15 at 19:58
  • possible duplicate of [Generating random numbers in Javascript in a specific range?](http://stackoverflow.com/questions/1527803/generating-random-numbers-in-javascript-in-a-specific-range) – depperm Jun 17 '15 at 20:00

1 Answers1

0

Well once you have generated the random number within a given range using any method it should be easy to assign any value you want based on the generated random value.

var min=1,max=3;
var x=Math.floor(Math.random()*(max-min+1)+min),chosen;
if(x==1)
    chosen='rock';
if(x==2)
   chosen='paper';
else
    chosen='scissor';
alert(chosen);
Community
  • 1
  • 1
meteor
  • 2,518
  • 4
  • 38
  • 52
  • Thank you!! That's what I was looking for. I couldn't remember the proper use of Math.floor and wasn't sure if I was using the right thing. As someone that's new to javascript I really appreciate your help! – Coodaye Jun 17 '15 at 20:23
  • I wouldn't convert the numbers to strings right away like that. Since you have some actual computing to do (assign to different players, figure out who wins, keep track of history, whatever), I'd do that with just the numbers themselves. You only need to convert 1 to "rock" when you display something to a human. – Lee Daniel Crocker Jun 17 '15 at 20:34
  • LeeDanielCrocker yeah that makes sense as having them as numbers is much more easy to do computation with! @Coodaye If it solves your problem do accept the answer! – meteor Jun 17 '15 at 21:14
  • http://imgur.com/3ws5RJ0 here's the entirety of what I'm trying to do accomplish. It's helpful but I need a way to compare a user answer to a random number. – Coodaye Jun 17 '15 at 22:04