Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Guessing the random number function...

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 03-21-2005, 01:33 AM PM User | #1 asharm27 New to the CF scene   Join Date: Mar 2005 Posts: 6 Thanks: 0 Thanked 0 Times in 0 Posts Guessing the random number function... Hi am am doing a bit of research and came up with a little game to demonstrate my website to school kids. My idea is that they type in a number in a text box and then they click a button to see if their guess matches the random number. If their guess is too low they get a window.prompt saying the no. is too low please enter another. If this guess is too low or high they get the same kind of window. I have made an attempt at the code but for some reason it doesnt work. I'd like to keep the code relatively simple (if possible) so that I can explain it to them. Is there any way someone might be able to help me please as i'm new at this game and pretty overwhelmed!!! Thanks in advance Arun The guess is entered in a simple text box in a form named 'form' and the action button then calls this function. function random(){ var temp; var randGuess; var random; var randomInt; var at1; var at2; at1 = window.prompt("Your guess is too low, please enter another guess","0"); at2 = window.prompt("Your guess is too high, please enter another guess","0"); random = math.floor(math.random()*11); randomInt = parseInt(random); randGuess = parseInt(document.form.randomNo.value); if(randGuess=randomInt){window.alert("Congratulations, you have guessed the random number correctly!!!")} if(temp=randomInt){window.alert("Congratulations, you have guessed the random number correctly!!!")} if(randGuess>randomInt) {temp = at2;} else{ if(randGuess
 03-21-2005, 01:54 AM PM User | #2 Bobo Regular Coder   Join Date: Jan 2004 Location: Port Huron, MI, U.S.A. Posts: 280 Thanks: 0 Thanked 0 Times in 0 Posts Not testing the code or anything , I noticed some syntax errors on youyr script. The color is the correction function random(){ var temp; var randGuess; var random; var randomInt; var at1; var at2; at1 = window.prompt("Your guess is too low, please enter another guess","0"); at2 = window.prompt("Your guess is too high, please enter another guess","0"); random = Math.floor(Math.random()*11); randomInt = parseInt(random); randGuess = parseInt(document.form.randomNo.value); if(randGuess==randomInt){window.alert("Congratulations, you have guessed the random number correctly!!!")} if(temp==randomInt){window.alert("Congratulations, you have guessed the random number correctly!!!")} if(randGuess>randomInt) {temp = at2;} else{ if(randGuess
 03-21-2005, 02:04 AM PM User | #3 asharm27 New to the CF scene   Join Date: Mar 2005 Posts: 6 Thanks: 0 Thanked 0 Times in 0 Posts Hi Bobo, I made the adjustments and still the code doesn't work. Do you happen to have any suggestions I could implement by any chance? I'd really appreciate any help. Thanks Arun
 03-21-2005, 11:28 AM PM User | #4 Brandoe85 teh Moderatorinator   Join Date: Sep 2004 Location: USA Posts: 2,472 Thanks: 4 Thanked 40 Times in 40 Posts You can play with this and change it to fit your needs: PHP Code: var num = Math.round(Math.random() * 11);   //Random number function guess() {     var input = parseInt(document.form.randomNo.value);  //The number guessed     var ran = document.form.randomNo;     if(input == num)   //Check to see if they guessed right     {         var yesNo = confirm("You guessed the right number!\nWould you like to play again?");  //prompt to ask if they want to play again         if(yesNo)    //if they chose to play again         {             location.reload();    //reload game             ran.value = '';        //set the value to nothing             return true;         }         else         {             return false;        //they didn't want to play again         }     }     if(input > num)        //the number is to high     {         alert("Your guess is to high! try again");         ran.select();    //give focus to the text box     }     if(input < num)        //the number is to low     {         alert("Your guess is to low! try again");         ran.select();    //give focus to text box     } }   Good luck; __________________ -Brando Why using tables for eating is stupid!

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 09:28 PM.