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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Does it matter what value we give the bet?

    Code:
    function rollthedice() //
    
    {
    
    money=100;
    
    bet=1000; 
    alert ("Play til you lose it all");
    
    while (money>=1)
    
    {
    
    //no betting more money than you have!
    
    //the line below will go forever...the if...break will
    
    //get you out of this infinite betting loop if you bet legally
    
    while (1==1)
    
    {
    
    pick=window.prompt("Please pick over 14(o) or under 14(u)","u");
    
    bet=parseInt(window.prompt("Please bet",money)); //
    if (bet<=money && bet>0) break; //
    
    alert("Illegal Betting detected!");
    
    }
    
    die1=Math.floor(Math.random()*6+1); 
    die2=Math.floor(Math.random()*6+1);
    
    die3=Math.floor(Math.random()*6+1);
    
    die4=Math.floor(Math.random()*6+1);
    
    //the *6 gives you 6 numbers and the 1 says start at 1 so
    
    //we randomly get numbers between 1 and 6 like on a die
    
    total=die1+die2+die3+die4;
    
    document.write("</br>You rolled "+die1+" and "+die2+" and "+die3+" and "+die4+" total = "+total);
    
    document.write("</br>You picked "+pick+" and rolled "+total);
    
    //you win if you pick u and you get less than 14
    
    //OR you pick o and you have more than 14
    
    if ((pick=="u" && total<14) || (pick=="o" && total>14))
    
    {
    
    money=money+bet;
    
    document.write(" You won! You now have $"+money);
    
    }
    
    else if (total==14)
    
    {
    
    document.write(" 14 is a tie...no winner..keep your bet");
    
    }
    
    else
    
    {
    
    money=money-bet;
    document.write(" You Lost! You now have $"+money);
    
    } //end of the if statement
    
    } //end of the---lets play WHILE we have more than $1 left
    
    alert ("Thanks for giving me all of your money!");
    
    } //end of the function
    
    </script>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,308
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    No. It should really read var bet; (Note the use of the var keyword which you have omitted throughout).

    Prompt is a very primitive way to obtain user input. Alerts should be used only for debugging.

    As has been explained a zillion times in this forum, document.write() is in effect obsolete. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

    In short, you are being taught to write antiquated and long-obsolete code! There are many other problems with your code, particularly the absence of any input verification.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 05-25-2012 at 09:17 AM.

    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.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,308
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    The great advantage of being on iBall's ignore list is that, although many of his posts simply repeat what has already been said, he cannot seize on something in one of my posts to start a pointless and belligerent argument.

    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
    •