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

    Angry still need help making button submit only five times

    tried to fix it with with the refresh() function but i dont know whats wrong i if u knowq what to do to make the guess button submit only five times please let me know. Basically what i want it to do is when u click the guess button five times an alert appears and says u have run out of guesses then the page reloads.
    heres the JS-

    <SCRIPT language=JavaScript>
    <!-- Begin
    var js_mult1=3141
    var js_mult2=5821
    var js_m1=100000000
    var js_m2=10000
    var js_iseed=0
    var js_iseed1=0
    var js_iseed2=0
    function random() {
    if (js_iseed == 0) {
    now = new Date()
    js_iseed = now.getHours() + now.getMinutes() * 60 + now.getSeconds() * 3600
    }
    js_iseed1 = js_iseed / js_m2
    js_iseed2 = js_iseed % js_m2
    var tmp = (((js_iseed2 * js_mult1 + js_iseed1 * js_mult2) % js_m2) *
    js_m2 + (js_iseed2 * js_mult2)) % js_m1
    js_iseed = (tmp + 1) % js_m1
    return (Math.floor((js_iseed/js_m1) * 100))
    }
    var nGuesses = 0
    function GuessNum() {
    var response
    var num = parseInt(document.forms[0].guess.value)
    document.forms[0].guess.value = num
    nGuesses++
    response = ""
    if (num < myNumber) response = response + "Higher!"
    if (num > myNumber) response = response + "Lower!"
    if (num == myNumber) {
    respone = "Correct!";
    alert ("Winner!! Congrats Click ok For your prize...")
    alert ("CzoneName: PW:")
    alert ("Remember only one prize per week....");
    }
    document.forms[1].result.value=response
    document.forms[1].guesses.value=nGuesses
    document.forms[0].guess.focus()
    document.forms[0].guess.select()
    return true
    }
    function GiveUp() {
    var response
    nGuesses = 0
    alert("Try it again you'll get it....")
    alert("Eventually????") ;
    }
    function refresh()
    {
    if (nGuesses<="5")
    {GuessNum()
    return true
    }
    else
    {alert('You have run out of chances'); window.location="http://www.freewebs.com/ctp/guess.htm"}
    }
    </SCRIPT>
    // End -->
    </SCRIPT>

    <META content="MSHTML 6.00.2900.2523" name=GENERATOR></HEAD>
    <BODY vLink=#0000ff bgColor=#ffffff onload=document.forms[0].guess.focus();document.forms[0].guess.select();refresh()/>
    <CENTER><BR>
    <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
    <TBODY>
    <TR>
    <TD align=middle width="99%"><BASEFONT><FONT size=6></B></FONT>
    <TABLE cellSpacing=0 cellPadding=3 width=486 border=0>
    <TBODY>
    <TR>
    <TD><FONT face=helvetica,arial,geneva></FONT></TD></TR></TBODY></TABLE>
    <CENTER>
    <H2>&nbsp;</H2>
    <P><FONT size=2><B>The object of this game is to guess the same number that the computer has guessed. The number will range from 1 to 100.
    <P></B></FONT>
    <SCRIPT>
    myNumber = random();
    nGuesses = 0
    </SCRIPT>

    <FORM onsubmit="GuessNum(); refresh(); return false">Your Guess: <INPUT size=5 name=guess> <INPUT type=button value=" Guess... " onclick="GuessNum(); refresh();"> <INPUT onclick=GiveUp(); type=button value=" GiveUp "> </FORM>
    <FORM>
    <CENTER>#-Guesses: <INPUT size=3 name=guesses> INFO: <INPUT size=10 name=result> </CENTER></FORM>

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Edinburgh, UK
    Posts
    402
    Thanks
    2
    Thanked 1 Time in 1 Post
    Okay the first problem was you had some syntax errors like </SCRIPT> twice.

    So then once I actually had it working you were calling the script twice so things were going up by 2 rather than one. So I reorganised a few things and changed the code a tad, but this now works

    Code:
    <html>
    <head>
    <title>temporary file</title>
    <SCRIPT language=JavaScript>
    <!-- Begin
    var js_mult1=3141;
    var js_mult2=5821;
    var js_m1=100000000;
    var js_m2=10000;
    var js_iseed=0;
    var js_iseed1=0;
    var js_iseed2=0;
    
    function random() {
    	if (js_iseed == 0) {
    		now = new Date()
    		js_iseed = now.getHours() + now.getMinutes() * 60 + now.getSeconds() * 3600
    		}
    	js_iseed1 = js_iseed / js_m2
    	js_iseed2 = js_iseed % js_m2
    	var tmp = (((js_iseed2 * js_mult1 + js_iseed1 * js_mult2) % js_m2) *
    	js_m2 + (js_iseed2 * js_mult2)) % js_m1
    	js_iseed = (tmp + 1) % js_m1
    	return (Math.floor((js_iseed/js_m1) * 100));
    }
    
    var nGuesses = 0;
    
    function GuessNum() {
    	var response
    	var num = parseInt(document.forms[0].guess.value)
    	document.forms[0].guess.value = num
    	nGuesses++
    	response = ""
    	if (num < myNumber) response = response + "Higher!"
    	if (num > myNumber) response = response + "Lower!"
    	if (num == myNumber) {
    		respone = "Correct!";
    		alert ("Winner!! Congrats Click ok For your prize...")
    		alert ("CzoneName: PW:")
    		alert ("Remember only one prize per week....");
    		}
    	document.forms[1].result.value=response
    	document.forms[1].guesses.value=nGuesses
    	document.forms[0].guess.focus()
    	document.forms[0].guess.select()
    	refresh();
    	return true
    }
    
    function GiveUp() {
    	var response
    	nGuesses = 0
    	alert("Try it again you'll get it....")
    	alert("Eventually????") ;
    }
    
    function refresh()
    {
    	if (nGuesses >= "5"){
    		alert('You have run out of chances');
    		window.location="file:///c:/Documents%20and%20Settings/Richard/Desktop/js.htm";
    	}
    }
    // End -->
    </SCRIPT>
    
    <META content="MSHTML 6.00.2900.2523" name=GENERATOR></HEAD>
    <BODY vLink=#0000ff bgColor=#ffffff onload="document.forms[0].guess.focus();document.forms[0].guess.select();">
    <CENTER><BR>
    <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
    <TBODY>
    <TR>
    <TD align=middle width="99%"><BASEFONT><FONT size=6></B></FONT>
    <TABLE cellSpacing=0 cellPadding=3 width=486 border=0>
    <TBODY>
    <TR>
    <TD><FONT face=helvetica,arial,geneva></FONT></TD></TR></TBODY></TABLE>
    <CENTER>
    <H2>&nbsp;</H2>
    <P><FONT size=2><B>The object of this game is to guess the same number that the computer has guessed. The number will range from 1 to 100.
    <P></B></FONT>
    <SCRIPT>
    myNumber = random();
    nGuesses = 0
    </SCRIPT>
    
    <FORM>Your Guess: <INPUT size=5 name=guess> <INPUT type=button value=" Guess... " onclick="javascript:GuessNum();"> <INPUT 
    
    onclick=GiveUp(); type=button value=" GiveUp "> </FORM>
    <FORM>
    <CENTER>#-Guesses: <INPUT size=3 name=guesses> INFO: <INPUT size=10 name=result> </CENTER></FORM>
    </body>
    </html>
    Hope this helps, and just to make things easier to read, could you use [ code ] [/ code ] (without the spaces) tags to wrap your code.
    Rich

    "An expert is a person who has made all the mistakes that can be made in a very narrow field."

  • #3
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    hey thanks a mil i see u added ur own js file on there to make it work, the only thing that wasnt added was the refreshing of the page. When it gets to the fifth submission and the alert pops up what should i add so the the page reloads or refresh.
    Hey thanks again

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Edinburgh, UK
    Posts
    402
    Thanks
    2
    Thanked 1 Time in 1 Post
    Nah It's all there. The problem that was happening was you were calling the GuessNum and REfresh when the button was presses but your refresh was also calling GuessNum. So I altered the Refresh to only move to a new location, and added the call from the end of GuessNum.

    Try it out and see if it works. When the user presses the button the 5th time, the page tells him higher/lower/correct and then automatically refreshes the page.
    Rich

    "An expert is a person who has made all the mistakes that can be made in a very narrow field."

  • #5
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    it doesnt refreshwhen u click the button five times, it just keeps saying u have run out of chances and the # of guesses keeps increasing
    guessing game check it out there thats were the code that u gave me is .
    Last edited by tamaisk; 01-10-2005 at 08:44 PM.

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Edinburgh, UK
    Posts
    402
    Thanks
    2
    Thanked 1 Time in 1 Post
    Yeah that's a simple one, I should have said change the window.location argument to be the page you want it to be. Currently it's pointing to a file on my computer, and really I should have switched it back to what you originally posted, but I forgot, sorry

    Changing that should make it work fine
    Rich

    "An expert is a person who has made all the mistakes that can be made in a very narrow field."


  •  

    Posting Permissions

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