5 Replies - 524 Views - Last Post: 17 March 2012 - 04:38 PM

#1 gamergrl22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 12

Guessing Game Problems

Posted 17 March 2012 - 08:58 AM

This it my first (and only) programming class I have so be gentle! I have to make a guessing game where the user chooses the max number to guess between 1 and that number, and then they guess until they get it while stating too high or too low. It works up until the random number part and giving the "too high", "too low" and "you win" prompts. Firebug said something was wrong with the == for the if (guess == random_number) part.

Thanks!



 
function ask (set_max) {

	var ask_max = prompt(set_max, "");
	return ask_max

}

var user_max = ask("Welcome! What number would you like to be the max?");

alert("The number you have chosen is " + user_max);

function random (random_number) {

	var com_answer = prompt(random_number, 1 + Math.floor(Math.random() * max))
	return com_answer

}

guess = prompt("Guess a number between 1 and " + user_max + "?");{

if (guess == random_number) {
	alert ("You've won!");  
}
else if (guess > random_number) {
	alert ("Too high! Guess again.");  
} 
else if (guess < random_number) {
	alert ("Too low! Guess again.");
} 
}



Is This A Good Question/Topic? 0
  • +

Replies To: Guessing Game Problems

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Guessing Game Problems

Posted 17 March 2012 - 09:26 AM

This is a Java forum. What you've posted is not Java.
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Guessing Game Problems

Posted 17 March 2012 - 09:48 AM

As I always say, Java != Javascript

Moved to the Javascript forum.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Guessing Game Problems

Posted 17 March 2012 - 01:27 PM

Indent your code and put semi-colons after all your statements.

guess = prompt("Guess a number between 1 and " + user_max + "?");{


Why is there a { after that line (and the extra closing } at the end)?

guess = prompt("Guess a number between 1 and " + user_max + "?");{

if (guess == random_number) {
	alert ("You've won!");  
}
else if (guess > random_number) {
	alert ("Too high! Guess again.");  
} 
else if (guess < random_number) {
	alert ("Too low! Guess again.");
} 


Where did the variable random_number come from? Did you perhaps forget to call a function?
Was This Post Helpful? 0
  • +
  • -

#5 gamergrl22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 12

Re: Guessing Game Problems

Posted 17 March 2012 - 04:01 PM

Sorry I didn't post this in the right one. Newbie!

As for the {/}'s firebug said it was wrong not to, well at least at the end anyways. It still says there is an error with the equal to statement. I screen shot it.Attached Image

Sorry and the var random_number comes from the function above it that makes the random number for the game. I'm not sure if I'm setting it up right or not. Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 gamergrl22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 12

Re: Guessing Game Problems

Posted 17 March 2012 - 04:38 PM

My teacher e-mailed me and sort of explained why it won't work. So, I've adjusted the code. I don't know if it was for the better, but here's hoping. It still doesn't work though. :(

 
function ask (set_max) {

	var ask_max = prompt(set_max, "");
	return ask_max

}

var user_max = ask("Welcome! What number would you like to be the max?");

	alert("Guess a number between 1 and " + user_max + "?");

function guessNumber(userGuess) {

	var rightNumber = (1 + Math.floor(Math.random() * max));
	return rightNumber

        	if (userGuess == rightNumber) {
                	alert("You've got it! You win! ^_^/>");
        	}
        	else if (userGuess > rightNumber) {
                	alert("You are too high. Guess again.");
        	}
        	else if (userGuess < rightNumber) {
                	alert("You are too low. Guess again.");
        	}
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1