0

I'm trying to write a pin number guessing game and wrote out a bunch of if else statements but it stops working after I input the first pin (correct or incorrect), can anyone tell me what's going on?

var ans = prompt("Do you want to play?");
if (ans == "y") {
	document.getElementById("ans").innerHTML = "You answered yes.";
	guessNum();
	if (gNum != pswd) {
		document.getElementById("hint").innerHTML = h1;
		guessNum();
		if (gNum != pswd) { 
			document.getElementById("hint").innerHTML = h2;
			guessNum();
			if (gNum != pswd) {
				document.getElementById("hint").innerHTML = h3;
				guessNum();
				if (gNum != pswd) {
					document.getElementById("hint").innerHTML = "You lost. :(";
				} else {
					document.getElementById("hint").innerHTML = "You guessed the pin!";}
			} else {
				document.getElementById("hint").innerHTML = "You guessed the pin!";}
		} else {
			document.getElementById("hint").innerHTML = "You guessed the pin!";}
	} else {
		document.getElementById("hint").innerHTML = "You guessed the pin!";}
} else {
	document.getElementById("ans").innerHTML = "You answered no.";}

Here is the rest of the javascript but I don't think the problem lies there.

var nums = [0, 0, 0, 0];
for (var idx = 0; idx < nums.length; ++idx)
{
    nums[idx] = Math.floor((Math.random() * 9) + 1);
}
pswd = nums.join("")
document.getElementById("nums").innerHTML = pswd;

function guessNum() {
var gNum = prompt("What do you think the number is?")
}

if (nums[3] % 2 == 0) {
	var divis = "even";
} else {
	var divis = "odd";
}
var h1 = "The first number is " + nums[0]
var h2 = "The sum of the middle numbers are " + (nums[1] + nums[2])
var h3 = "The last number is " + divis

7
  • Can you please tell where in the code you process user entry? Commented Feb 8, 2015 at 4:18
  • It's just a prompt window that assigns whatever the user inputs into a variable. var ans = prompt("Do you want to play?"); Commented Feb 8, 2015 at 4:20
  • If I can offer a suggestion, your IF-ELSE statement is very crowded. Why not start with the first IF statement, and testing how that reacts? Because at the moment it's hard to figure out exactly which element is the problem. Commented Feb 8, 2015 at 4:27
  • Fixed it for ya. jsfiddle.net/8osnr8er :-) Commented Feb 8, 2015 at 4:54
  • @AdibBehjat I have tested it completely along the way and the first few statements work i'm just not sure what's going wrong after. Commented Feb 8, 2015 at 5:01

1 Answer 1

2

The problem is that the variable gNum that you're creating inside the guessNum function only lives there. Javascript is function scoped.

When you get to the line if (gNum != pswd) {... gNum simply doesn't exist. Make guessNum return the value, and create the actual gNum variable on the same scope as the if.

function guessNum() {
  return prompt("What do you think the number is?")
}

...
var gNum = guessNum();
if (gNum != pswd) {
...

I would also advise you to study while loops, to avoid these nested ifs. Keep the hard work! :)

Sign up to request clarification or add additional context in comments.

2 Comments

I tried doing it as a while statement but I had a lot of trouble figuring it out how to make a random hint pop up if the user didn't guess the pin number correctly, I tried setting up an array with the hints in it, drawing a random number, then using that for the array, I was trying to set up another array along side that which wouldn't allow me to give the user the same hint again, or removing it from the array but I dropped it all together and make the ridiculous nested if elses. Thank you for this suggestion! I still have a lot to learn.
It is easier than it seems. A quick scratch that may help you: jsfiddle.net/jL8jexnq

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.