Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Rock, Paper, Scissors game

    Hi, I began JavaScript on Codeacademy a couple of days ago and so far it's been okay. But I've run into a problem designing a rock, paper, scissors game for one of the exercises. I tried posting my question on the Codeacademy forums but no one has replied. This is my code for the game. Currently, the randomized computer choice function works. But as you can see, I've only used < and >. Therefore, if the computer choice is somehow exactly .33 or .66, the computer won't be able to make a decision because I have not told it which choice to pick. However, when I add a <= or >=, I get an ") expected" error. How can i change my code to include .33 and .66?

    var userChoice = prompt("Do you choose rock, paper, or scissors?");
    var computerChoice = Math.random();
    console.log(computerChoice);
    if(0 < computerChoice < .33) {
    computerChoice = "rock"
    }
    else if(.33 < computerChoice < .66) {
    computerChoice = "paper"
    }
    else {
    computerChoice = "scissors"
    }

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,451
    Thanks
    17
    Thanked 275 Times in 275 Posts
    You need to use the logical operators for less than, more than, and for AND and OR as well. So study up on the logical operators.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,692
    Thanks
    25
    Thanked 657 Times in 656 Posts
    if(computerChoice <= .33) {
    computerChoice = "rock"
    }else if(computerChoice <= .66) {
    computerChoice = "paper"
    }else {
    computerChoice = "scissors"
    }
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #4
    New Coder
    Join Date
    Aug 2014
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post
    Not really sure if that's what you're looking for but the code below works under Firefox and Webkit.

    Code:
    var userChoice = prompt("Do you choose rock, paper, or scissors?");
    var computerChoice = Math.random();
    console.log(computerChoice);
    if(0 <= computerChoice && computerChoice <= .33) {
    computerChoice = "rock"
    }
    else if(computerChoice > .33 && computerChoice <= .66) {
    computerChoice = "paper"
    }
    else {
    computerChoice = "scissors"
    }

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by azimdj5 View Post
    However, when I add a <= or >=, I get an ") expected" error. How can i change my code to include .33 and .66?
    Are you sure that your syntax is correct >= and <=, not =< or =>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Use
    Code:
    var computerChoice = Math.ceil(Math.random()*3);
    so that you have a choice of 1, 2 or 3 and then you can use ==
    Code:
    if (computerChoice == 1) {
        computerChoice = "rock";
    }
    else if (computerChoice == 2) {
        computerChoice = "paper";
    }
    else {
        computerChoice = "scissors";
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    Quote Originally Posted by glenngv View Post
    Use
    Code:
    var computerChoice = Math.ceil(Math.random()*3);
    so that you have a choice of 1, 2 or 3 and then you can use ==
    Ummm...not entirely true. There is a VANISHINGLY SMALL but still finitely possible chance that Math.random() can be *EXACTLY* zero!

    And so it is POSSIBLE that the value for computerChoice using your code will be zero, not 1 or 2 or 3.

    This admittedly very small chance for a zero result is the reason that code would be better written as
    Code:
        var computerChoice = 1 + Math.floor(Math.random()*3);
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    Of course, the best way to write it (well..."best" in the sense of most obscure to a newbie programmer <grin/>) would maybe be:
    Code:
    var computerChoice = ["rock","scissors","paper"][ Math.floor( Math.random() * 3 ) ];
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by Old Pedant View Post
    Ummm...not entirely true. There is a VANISHINGLY SMALL but still finitely possible chance that Math.random() can be *EXACTLY* zero!

    And so it is POSSIBLE that the value for computerChoice using your code will be zero, not 1 or 2 or 3.

    This admittedly very small chance for a zero result is the reason that code would be better written as
    Code:
        var computerChoice = 1 + Math.floor(Math.random()*3);
    Good catch.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •