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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hi Guys! 2nd day in the world of Java, and tearing my hair out with this. Any help ?

    hmmmkay. Basically this is a simple rock paper scissors game just to get myself familiar with basic functions and statements.

    var userInput = prompt("Please choose rock, paper or scissors. Lowercase.");
    var cpuInput = Math.random();

    if (cpuInput <= 0.34){
    cpuInput = "rock";
    }else if(cpuInput >= 0.67){
    cpuInput = "paper";
    }else{ cpuInput = "scissors";}

    var compare = function(Choice1,Choice2){

    if (Choice1===Choice2){
    return "It's a draw!";
    }

    if (Choice1==="rock") {
    if (Choice2==="paper") {
    return "Player wins!";
    } else {
    return "Computer picks paper and wins!";
    }
    if (Choice1==="paper") {
    if (Choice2==="scissors") {
    return "Player wins!";
    }else{
    return "Computer picks scissors and wins!";
    }

    if (Choice1==="scissors"){
    if (Choice2==="rock"){
    return "Player wins!";
    }else{
    return "Computer picks rock and wins!";
    }
    }
    }
    }
    };
    compare(userInput,cpuInput);


    Now, here's the problem with it. It only accepts a draw for either scissors or paper. The rock works perfectly and will show the correct outcome each time, but rock and paper only show the draw or nothing at all. I'm doing this on the codeacdemy web app if that makes a difference. The first statement is fine, as you can see if you add a console.log line - it's selecting as required. PLEASE HELP!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    First of all, this is *NOT* your "2nd day in the world of Java."

    Unless I miss my guess entirely, you have not yet even seen Java.

    This code is JavaSCRIPT. Java and JavaScript are entirely different languages. (In general, Java is much harder to learn, certainly when getting started.)

    Oh...and do yourself a *HUGE* favor: Learn to indent your code properly. If you actually did so and it just didn't show up that way here, simply use your mouse to highlight the code part of your post and then click on the "#" icon in the top of the editor window.


    Anyway...

    Do you care that you are using ancient history JavaScript? prompt( ) and alert( ) are considered obsolete.
    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.

  • #3
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Your ifs are nested incorrectly and perhaps your logic is incorrect(?), but it is messy to work out unless you enclose your code in CODE tags (use the hash # sign) and indent the code neatly - so we (and you) can see clearly where the brackets open and close.

    IMO prompt() and alert() are still useful tools when just starting out.
    Last edited by AndrewGSW; 12-18-2012 at 12:04 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Actually, your whole problem is that some of your { and } are in the wrong places.

    *IF* you had properly indented your code, you would have seen this.
    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    No. I take it back. It is *NOT* just indentation.

    Your code actually makes no sense.

    I do have to ask: Why bother to have a separate compare function if you then name the two arguments to it just Choice1 and Choice2???

    Wouldn't it be more logical to name them something like user and computer?

    If you do that you see *some* of your bugs IMMEDIATELY!
    Code:
    function compare( user, computer )
    {
        if ( user === computer )
        {
            return "It's a draw!";
        }
        if ( user  ==="rock") 
        {
            if ( computer ==="paper" ) 
            {
                 return "Player wins!"; //??? rock covers paper???
            } else { 
                 return "Computer picks paper and wins!"; // A FLAT OUT LIE!
           }
       }
    ...
    Look there: The user *HAS* chosen rock.

    Now you say "what happens if the computer chooses paper"?

    As your code is written, you say that rock for user and paper for computer means the player wins!

    BUT WORSE:

    As your code is written, you say that else if the computer *DID NOT CHOOSE PAPER* (after the user chose rock) then the result is that the computer *DID* choose paper! IMPOSSIBLE! Your else says the computer DID NOT DO SO.

    }
    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.

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,775
    Thanks
    55
    Thanked 519 Times in 516 Posts
    a shortcut to get rock, paper or scissors (not entirely random, but close enough for the exercise, I reckon):

    Code:
    <script>
    var cpuInput = ["rock","paper","scissors"][Math.floor(Math.random()*3)];
    alert(cpuInput)
    </script>

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    ?? Why do you say "not entirely random"? True, the random number generator in JavaScript doesn't satisfy any serious criteria, especially when it comes to sequences, repetitions, etc., but it *IS* random when making a single random selection.
    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
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Here. I know you can't turn this in, as your instructor will be expecting you to write ancient history JavaScript since he/she apparently hasn't bothered to learn modern JavaScript. But this is one way you could do it in modern JS.
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Rock, Paper, Scissors Game</title>
    <style type="text/css">
    span { font-weight: bold; color: blue; }
    </style>
    </head>
    <body>
    <form>
    Make your choice:
        <input type="button" name="user" value="Rock">
        <input type="button" name="user" value="Paper">
        <input type="button" name="user" value="Scissors">
    <hr>
    Your choice: <span id="userChoice"></span><br>
    Computer choice: <span id="computerChoice"></span><br>
    Winner: <span id="winner"></span>
    </form>
    
    
    <script type="text/javascript">
    (
      function()
      {
          var form = document.forms[0];
          var uMsg = document.getElementById("userChoice");
          var cMsg = document.getElementById("computerChoice");
          var wMsg = document.getElementById("winner");
    
          var btns = form.user;
          for ( var b = 0; b < btns.length; ++b )
          {
              btns[b].onclick = play; 
          }
          function play( )
          {
              var u = this.value;
              var c = ["Rock","Paper","Scissors"][Math.floor(Math.random()*3)];          
              uMsg.innerHTML = u;
              cMsg.innerHTML = c;
              wMsg.innerHTML = ( u == c ) ? "DRAW"
                             : (    u=="Paper"    && c=="Rock" 
                                 || u=="Rock"     && c=="Scissors"
                                 || u=="Scissors" && c=="Paper" ) ? "PLAYER"
                             : "COMPUTER";
         }
      }
    )();
    </script>
    </body>
    </html>
    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
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,483
    Thanks
    0
    Thanked 635 Times in 625 Posts
    Quote Originally Posted by AndrewGSW View Post
    IMO prompt() and alert() are still useful tools when just starting out.
    Yes - they are useful debugging tools for anyone who has not yet learnt where to find the error console and debugger in their browser. (or who hasn't yet installed a debugger in Firefox)

    A simple HTML form is far more suitable for data input and innerHTML more suitable for outputting anything than using debugging dialog boxes are because:

    1. you don't get the checkboxes asking if you want to disable further dialogs (in Firefox and Chrome) or disable JavaScript (in Opera). Those checkboxes were added once the dialogs had become obsolete for use in live scripts and were repurposed as a simple debugging tool - although actually using the error console is actually far easier.
    2. Your script actually interacts with the web page as all JavaScript since 2005 can and should. Which means that you are 1000% closer to being able to write real JavaScript suitable for use on the web.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    EDIT: I'm an idiot
    Last edited by piemanXD; 12-18-2012 at 03:48 PM. Reason: I'm an idiot with a bad temper

  • #11
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Actually, your whole problem is that some of your { and } are in the wrong places.

    *IF* you had properly indented your code, you would have seen this.
    EDIT: I'm an idiot
    Last edited by piemanXD; 12-18-2012 at 03:48 PM. Reason: I'm an idiot with a bad temper

  • #12
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm sorry for being a douchebag everybody
    Last edited by piemanXD; 12-18-2012 at 03:49 PM.

  • #13
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,775
    Thanks
    55
    Thanked 519 Times in 516 Posts
    Quote Originally Posted by piemanXD View Post
    Old pedant, if you don't want to help, don't post. Don't be a miserable prick. I'd rather do without you.
    The guy rewrites your entire code for you and that's your response? I don't think you'll have to worry too much about who responds to your posts in future, silly rabbit.

  • #14
    New Coder donna1's Avatar
    Join Date
    Nov 2012
    Location
    london
    Posts
    99
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by piemanXD View Post
    Old pedant, if you don't want to help, don't post. Don't be a miserable p...k. I'd rather do without you.
    How rude. Old Pedant is the nicest guy on this site and has helped me many times.

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,735
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by xelawho View Post
    The guy rewrites your entire code for you and that's your response? I don't think you'll have to worry too much about who responds to your posts in future, silly rabbit.
    Is is always sad when immature juvenile newcomers think that they can insult senior members. The inevitable outcome is a destroyed reputation meaning that no-one is going to take time to help you in the future lest they get a mouthful. You catch more flies with honey than with vinegar. When you grow up you will learn that rudeness and bad language never bring beneficial results.

    Code:
    var rude = true;
    var thanks = false;
    if (rude && !thanks) {
    var interestLost = true;
    var moreHelp = 0;
    }

    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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