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
    New Coder
    Join Date
    Feb 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Help with random number Game

    Im creating a random number game w/ javascript, but it keeps going into either an infinite loop or no loop at all. could someone please help here is my code:
    <script type = "text/javascript">
    <!--
    // variables used to test the state of the game
    var numberToGuess = 0,
    latestGuess = 9999,
    result,
    numOne;



    numberToGuess = randomNum
    while (numberToGuess != latestGuess){
    result= window.prompt(
    "Enter a number", "0");

    latestGuess = parseInt( result );
    if (latestGuess = numberToGuess )
    alert("CONGRATULATIONS, reload to restart");
    else
    if (latestGuess > numberToGuess)
    alert("to high");
    else
    if (latestGuess < numberToGuess)
    alert("to low");
    }

    function randomNum()
    {


    numOne=Math.floor ( 1 + Math.random() * 1000);

    return numOne
    }
    // -->
    </script>

  • #2
    New Coder
    Join Date
    Feb 2009
    Location
    Jakarta, Indonesia
    Posts
    93
    Thanks
    2
    Thanked 20 Times in 20 Posts
    You forgot to call the randomNum function:

    Code:
    var latestGuess = 9999;
    var result;
    var numberToGuess = randomNum();
    //document.title = numberToGuess;
    while (numberToGuess != latestGuess){
    	result= window.prompt("Enter a number", 5);
    	latestGuess = parseInt(result);
    	if (latestGuess == numberToGuess ){
    		alert("CONGRATULATIONS, reload to restart");
    	}else{
    		if (latestGuess > numberToGuess){
    			alert("to high");
    		}else{
    			if (latestGuess < numberToGuess){
    				alert("to low");
    			}
    		}
    	}
    }
    function randomNum(){
    	var numOne=Math.floor ( 1 + Math.random() * 1000);
    	return numOne
    }

  • Users who have thanked gun5 for this post:

    ryukenblaze (02-07-2009)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,175
    Thanks
    203
    Thanked 2,550 Times in 2,528 Posts
    A good many errors, I fear. You need to have a look at the use of () brackets and {} braces.

    Try this (and learn from it, please!)

    Code:
    <script type = "text/javascript">
    
    function playGame() {
    // variables used to test the state of the game
    var numberToGuess = 0;
    var latestGuess = 0;
    var numberToGuess = Math.floor (1 + Math.random() * 100);  
    
    while (numberToGuess != latestGuess){
    var latestGuess = window.prompt("Enter a number", "");
    if (latestGuess == null) {
    alert ("Goodbye!");
    return false;  // end if "Cancel" button pressed
    }
    
    latestGuess = parseInt(latestGuess);
    
    if (isNaN(latestGuess)) {
    alert ("Invalid entry - you must enter a number");
    } 
    if (latestGuess == numberToGuess ) {
    alert("CONGRATULATIONS, reload to restart");
    return false;
    }
    if (latestGuess > numberToGuess) {
    alert("Too high - try again");
    }
    if (latestGuess < numberToGuess) {
    alert("Too low - try again");
    }
    
    }
    }
    
    playGame()
    </script>

    "In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

  • #4
    Regular Coder MrEnder's Avatar
    Join Date
    Jul 2008
    Location
    Canada Ontario
    Posts
    258
    Thanks
    18
    Thanked 4 Times in 4 Posts
    Your guys tabbing makes me cry sooo hard to read badly tabbed coding ><

  • #5
    New Coder
    Join Date
    Feb 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thanks for the help

    Thanks everyone for the help. Im a noob, so expect more bad code from me in the future


  •  

    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
    •