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

    Java Script Problem

    Hiya all, I'm new here and fairly new to JavaScript and coding - one of my teachers said it would be a fun language to learn to help me out when our GCSE's come up so I thought I would give it a go. I am currently trying to make a roulette style of game - the first phase though is fairly basic. I want the user to click a number and then to be told if it is right or wrong and update their chips. Currently I am using a fixed number whilst building up the script for testing purposes - I will change that to a random number later on. So far I have broke the code down and the parts that work are where the user function is not there(The buttons) so i assume the function for changing a variable to what the user picks is broke. Although when printing that function it says that it has changed. Like I said I'm new here so if this is not where to post for help just let me know and point me in the wrong direction any help would be much appreciated

    The code is :
    Code:
     
    <html>
    <body>
    <script language='JavaScript'>
    
    var numbers = new Array(39);
    numbers[0] = "BLANK IF THIS COMES UP PANIC AND RUN";
    numbers[1] = "1";
    numbers[2] = "2";
    numbers[3] = "3";
    numbers[4] = "4";
    numbers[5] = "5";
    numbers[6] = "6";
    numbers[7] = "7";
    numbers[8] = "8";
    numbers[9] = "9";
    numbers[10] = "10";
    numbers[11] = "11";
    numbers[12] = "12";
    numbers[13] = "13";
    numbers[14] = "14";
    numbers[15] = "15";
    numbers[16] = "16";
    numbers[17] = "17";
    numbers[18] = "18";
    numbers[19] = "19";
    numbers[20] = "20";
    numbers[21] = "21";
    numbers[22] = "22";
    numbers[23] = "23";
    numbers[24] = "24";
    numbers[25] = "25";
    numbers[26] = "26";
    numbers[27] = "27";
    numbers[28] = "28";
    numbers[29] = "29";
    numbers[30] = "30";
    numbers[31] = "31";
    numbers[32] = "32";
    numbers[33] = "33";
    numbers[34] = "34";
    numbers[35] = "35";
    numbers[36] = "36";
    numbers[37] = "0";
    numbers[38] = "00";
    
    var goldennum = numbers[20]; //roullete number
    document.write(goldennum +"</br>"); 
    
    var userpot = 500; // The amount of credits or chips the user has
    //var userpot = userpot*38; 
    //document.write(userpot);   user pot working calc
    
    var usernum = 200; //user betting number
    
    							function button00(){
    							var usernum = 00;
    							
    					        }
    
    							function button0(){
    							var usernum = 0;
    							
    							}
    
    							function button3(){
    							var usernum = 3;
    							
    							}
    
    							function button6(){
    							var usernum = 6;
    							
    							}
    
    							function button9(){
    							var usernum = 9;
    							
    							}
    
    							function button12(){
    							var usernum = 12;
    							
    							}
    
    							function button15(){
    							var usernum = 15;
    							
    							}
    
    							function button18(){
    							var usernum = 18;
    							
    							}
    
    							function button21(){
    							var usernum = 21;
    							
    							}
    
    							function button24(){
    							var usernum = 24;
    							
    							}
    
    							function button27(){
    							var usernum = 27;
    							
    							}
    
    							function button30(){
    							var usernum = 30;
    							
    							}
    
    							function button33(){
    							var usernum = 33;
    							
    							}
    
    							function button36(){
    							var usernum = 36;
    							
    							}
    
    							function button2(){
    							var usernum = 2;
    							
    							}
    
    							function button5(){
    							var usernum = 5;
    							
    							}
    
    							function button8(){
    							var usernum = 8;
    							
    							}
    
    							function button11(){
    							var usernum = 11;
    							
    							}
    
    							function button14(){
    							var usernum = 14;
    							
    							}
    
    							function button17(){
    							var usernum = 17;
    							
    							}
    
    							function button20(){
    							var usernum = 20;
    							//document.write("</br></br></br>" + usernum + "</br></br></br>");
    							}
    
    							function button23(){
    							var usernum = 23;
    							
    							}
    
    							function button26(){
    							var usernum = 26;
    							
    							}
    
    							function button29(){
    							var usernum = 29;
    							
    							}
    
    							function button32(){
    							var usernum = 32;
    							
    							}
    
    							function button35(){
    							var usernum = 35;
    							
    							}
    
    							function button1(){
    							var usernum = 1;
    
    							}
    
    							function button4(){
    							var usernum = 4;
    							
    							}
    
    							function button7(){
    							var usernum = 7;
    							
    							}
    
    							function button10(){
    							var usernum = 10;
    							
    							}
    
    							function button13(){
    							var usernum = 13;
    							
    							}
    
    							function button16(){
    							var usernum = 16;
    							
    							}
    
    							function button19(){
    							var usernum = 19;
    							
    							}
    
    							function button22(){
    							var usernum = 22;
    							
    							}
    
    							function button25(){
    							var usernum = 25;
    							}
    
    							function button28(){
    							var usernum = 28;
    							
    							}
    
    							function button31(){
    							var usernum = 31;
    							
    							}
    
    							function button34(){
    							var usernum = 34;
    							
    							}
    							
    							
    							
    function runnums(){					//function to run the if the users bet is equal to the roulette number		
    if(usernum == goldennum){
                 var userpot=500;
    			 userpot = userpot * 38;
    			 document.write(userpot);
    		     document.write(  "   YOU ARE A WINNER   ");
    							} else{
    								 var userpot = 500;
    								 userpot= userpot - userpot; 
    								document.write ("You have " + userpot + " credits left ");
    							    document.write ( "   YOU ARE A LOOOOOSER "); 
    							}  
    }
    
    </script>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
    						  <input type="button" name="Button" value="00" onclick="button00()">
    						  <input type="button" name="Button3" value="3" onclick= "button3()"> 
    						  <input type="button" name="Button6" value="6" onclick= "button6()"> 
    						  <input type="button" name="Button9" value="9" onclick= "button9()"> 
    						  <input type="button" name="Button12" value="12" onclick="button12()">
    						  <input type="button" name="Button15" value="15" onclick="button15()"> 
    						  <input type="button" name="Button18" value="18" onclick="button18()">
    						  <input type="button" name="Button21" value="21" onclick="button21()">
    						  <input type="button" name="Button24" value="24" onclick="button24()">
    						  <input type="button" name="Button27" value="27" onclick="button27()">
    						  <input type="button" name="Button30" value="30" onclick="button30()">
    						  <input type="button" name="Button33" value="33" onclick="button33()">
    						  <input type="button" name="Button36" value="36" onclick="button36()">
    <p>
    						  <input type="button" name="Button0" value="0"   onclick="button0()"> 
    						  <input type="button" name="Button2" value="2"   onclick="button2()">
    						  <input type="button" name="Button5" value="5"   onclick="button5()">
    						  <input type="button" name="Button8" value="8"   onclick="button8()">
    						  <input type="button" name="Button11" value="11" onclick="button11()">
    						  <input type="button" name="Button14" value="14" onclick="button14()">
    						  <input type="button" name="Button17" value="17" onclick="button17()">
    						  <input type="button" name="Button20" value="20" onclick="button20()">
    						  <input type="button" name="Button23" value="23" onclick="button23()">
    						  <input type="button" name="Button26" value="26" onclick="button26()">
    						  <input type="button" name="Button29" value="29" onclick="button29()">
    						  <input type="button" name="Button32" value="32" onclick="button32()">
    						  <input type="button" name="Button35" value="35"   onclick="button35()">
    <p>
    							<input type="button" name="Button1" value="1"   onclick="button1()"> 
    							<input type="button" name="Button4" value="4"   onclick="button4()"> 
    							<input type="button" name="Button7" value="7"   onclick="button7()">
    							<input type="button" name="Button10" value="10"  onclick="button10()">
    							<input type="button" name="Button13" value="13"  onclick="button13()">
    							<input type="button" name="Button16" value="16"  onclick="button16()">
    							<input type="button" name="Button19" value="19"  onclick="button19()">
    							<input type="button" name="Button22" value="22"  onclick="button22()">
    							<input type="button" name="Button25" value="25" onclick="button25()">
    							<input type="button" name="Button28" value="28" onclick="button28()">
    							<input type="button" name="Button31" value="31" onclick="button31()">  
    							<input type="button" name="Button34" value="34" onclick="button34()">
    							<input type="button" name="Place Bets" value="Place Bets" onclick="runnums()">
    
    </body>
    </html>
    Last edited by WA; 03-09-2012 at 07:14 PM.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    document.write can only be run before the web page finishes loading - also it became obsolete in about 2005 when Netscape 4 dies.

    language=javascript was done away with long ago - to have the script work in all browser you should now use type="text/javascript" - even that has been labelled as obsolete but the correct type="application/javascript" doesn't work in IE8 or earlier.

    JavaScript is best added to the bottom of the body so that the rest of the page loads first - that way you can use either innerHTML or the proper DOM commands to interact with the page.

    You might also look at making sure your HTML is valid as JavaScript doesn't always work as expected if there are errors in the HTML.
    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.


  •  

    Tags for this Thread

    Posting Permissions

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