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 5 of 5
  1. #1
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts

    Simple script does not work in Google Chrome

    I wrote the following simple code to answer a student's request for help in this forum. It works fine in IE (my preferred browser) but in Chrome the alert showing the computer's choice does not always work or display. I am baffled!

    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    </head>
    <body>
    
    <script type = "text/javascript">
    //WORKS PROPERLY IN IE ONLY
    
    var numstones = 21;
    
    while (numstones > 0) {
    
    //PLAYER
    for (var i=1; i<2; i++) {
    var ans = parseInt(prompt ("How many stones do you wish to remove?", ""),10);
    if ((isNaN(ans)) || (ans == "") || (ans <1) || (ans >3)) {
    alert ("You must enter a number 1-3!!  ");
    i -- ;
    }
    }
    
    if (ans > numstones) {ans = numstones}
    numstones = numstones - ans;
    window.alert ("You have removed " + ans + " stones. \nThere are " + numstones + " stones left.");
    if (numstones == 0) {
    window.alert ("YOU HAVE TAKEN THE LAST STONE  - YOU LOSE!");
    break;
    }
    
    //COMPUTER
    if (numstones >=2 && numstones <=4) {
    var computerMove = numstones - 1;
    }
    else {
    var computerMove = Math.floor(Math.random()* 3 +1);
    if (computerMove > numstones) {computerMove = numstones}
    }
    numstones = numstones - computerMove;
    // In Chrome this alert does not always show
    window.alert ("The computer has chosen to remove " + computerMove + " stones.\nThere are " + numstones + " stones left.");
    if (numstones == 0) {alert ("COMPUTER TAKES LAST STONE AND  LOSES!")}
    }
    
    </script>
    
    
    </body>
    </html>
    Comments welcome!
    Last edited by Philip M; 02-28-2014 at 07:23 AM. Reason: Correctiion

    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.

  • #2
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    Hi

    I tried 10 games, reloaded browser twice (just to test), using chrome latest version

    worked fine.

    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • Users who have thanked low tech for this post:

    Philip M (02-27-2014)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Thanks Low Tech! I have re-installed Chrome and it is now working fine for me as well!

    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.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    You might have accidentally checked the "Prevent this page from creating additional dialogs" checkbox in the alert.

    Btw, you have a typo in the prompt:

    Code:
    var ans = parseInt(prompt("How many stones do you wish to remove?", "", 10));
    The base 10 for the parseInt was inadvertently set as the third parameter for the prompt(). In most situations, this will not cause any error in the logic but if you enter "0xF", then you will be surprise of the output.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by glenngv View Post
    You might have accidentally checked the "Prevent this page from creating additional dialogs" checkbox in the alert.
    No, I did check that. There was something wrong with my copy of Chrome.

    Quote Originally Posted by glenngv View Post
    Btw, you have a typo in the prompt:

    Code:
    var ans = parseInt(prompt("How many stones do you wish to remove?", "", 10));
    The base 10 for the parseInt was inadvertently set as the third parameter for the prompt(). In most situations, this will not cause any error in the logic but if you enter "0xF", then you will be surprise of the output.
    Thanks, corrected.

    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.


  •  

    Posting Permissions

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