Thread: Help Needed

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Help Needed


    Hi. Im attempting to make a Rock Paper Scissors game using mainly arrays but I cannot figure out how to randomize the selections of the computer. Here is the code below any help would be greatly appreciated.

    <html>
    <head>
    <title>JavaScript guess number game</title>
    </head>
    <body>
    <script type="text/javascript">
    answerArray = new Array(3);
    answerArray[1]="Rock";
    answerArray[2]="Paper";
    answerArray[3]="Scissors";
    comArray = new Array(3);
    comArray[1]="Rock";
    comArray[2]="Paper";
    comArray[3]="Scissors";
    var user_answer = 0;
    var com_answer = 0;
    user_answer = prompt("Enter 1 (Rock), 2 (Paper), 3 (Scissors) ",0);
    if (user_answer == com_answer)
    {
    document.write("You got it");
    }
    else
    {
    document.write("wrong guess");
    }


    </script>
    </html>
  2. #2
  3. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    106
    Rep Power
    4
    Look here:

    http://www.w3schools.com/jsref/jsref_random.asp

    Multiply result of random call by 3 and use parseInt to chop off the fraction.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Please don't link to w3schools

    This is one of the few topics they didn't get wrong, but generally this is a terrible source with wrong and obsolete information, bad practices and plain nonsense. So please don't promote that site. They are not affiliated with the W3C in any way. They just use that name to -- I guess -- sell their useless certificates. Check w3fools.com for some concrete examples of why this site is not suitable for learning.

    A good resource for JavaScript is the Mozilla developer network. The documenation of Math.random() includes a function for random integers.

    Some comments on the code:
    • Array indices in JavaScript start with 0. Using 1 as the first index will lead to confusion and unexpected results. For example, your array length is actually 5 with the first element (index 0) being undefined. The element at index 4 is the fifth element. Arrays in JavaScript grow dynamically (which makes explicit length declarations pretty much useless).
    • Do not use this new Array(...) stuff. I don't know where you got it from, but nobody uses that except bad tutorials. Arrays in JavaScript are created with array literals consisting of square brackets and a list of the elements. In your case: ['Rock', 'Paper', 'Scissors']. This way you also don't have to worry about the indices.
    • Do not use document.write(). It has several problems and is yet another feature only bad tutorials use. In this simple example it's OK, but when you start writing actual code, forget this method.
    • Please wrap your code in [ CODE ] tags to keep the formatting and make it readable for us.
    Last edited by Jacques1; April 16th, 2013 at 04:52 PM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    106
    Rep Power
    4
    Thanks for the info about the Mozilla developer network. Looks like a much better site.
  8. #5
  9. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,158
    Rep Power
    183
    @coolcat3, I corrected some things (in your example); I created two new text variables, and added the right/wrong messages, to them. I removed write() and replaced it with appendChild(). I also added the round() and rand() Global Math functions, along with adding/increasing (that random number) by one integer, to that over all number; to create a random number from 1 to 3. The arrays were not needed, at all, to replicate; what you were wanting to do, so I 86ed (removed) them too. I also added a window load event, to trigger the function (which I wrapped your code in). Check out this modified example of your code; look at the changes and if you have questions... we can go from there.
    Code:
    <script type="text/javascript">
    
    var user_answer, txt;
    
    var right = "You got it";
    var wrong = "wrong guess";
    
    function rps()
    {
    var com_answer = Math.round(Math.random()*2) + 1;
    var user_answer = prompt("Enter 1 (Rock), 2 (Paper), 3 (Scissors) ",0);
    if (user_answer == com_answer)
     {
      txt = document.createTextNode(right);
      document.getElementsByTagName("body")[0].appendChild(txt);
     }
    else
     {
      txt = document.createTextNode(wrong);
      document.getElementsByTagName("body")[0].appendChild(txt);
     }
    }
    
    window.onload = function() {
     rps();
    }
    
    </script>
    Last edited by web_loone08; April 16th, 2013 at 09:09 PM. Reason: Forgot to mention a couple changes, that I made.

IMN logo majestic logo threadwatch logo seochat tools logo