0

Ok, I've been up all night try to get this working and i'm a complete javascript newbie

var Hunger=8;

var interval = setInterval( increment, 10000);

    function changeImage(a) {
        document.getElementById("img").src=a;
    window.setTimeout(goIdle,20000)
    }
function goIdle() {
    document.getElementById("img").src="idle.gif";

function increment(){
        Hunger = Hunger % 24 + 1;
    if (Hunger >= 24)
    }
    if (Hunger >= 12)
        changeImage("cry.gif")
    }
}

function eat() {
    if (Hunger == 6) {
        changeImage("love.gif");
        var Hunger=0
    }
    else {
        ...
    }
}

What happens is that when I press the button to trigger it,

    <input type="button" value="Eat" onclick='eat();' /> 

It changes the image but the hunger doesn't go down

4
  • 1
    Change "var Hunger=0" to "Hunger=0;" Commented Aug 20, 2013 at 12:20
  • if (Hunder >= 24) } is pointless, goIdle has one } too much, or that last if-statement is missing the {. Commented Aug 20, 2013 at 12:22
  • You are creating a new local variable in another scope, hiding the field Hunger from the outer scope instead of actually modifying your Hunger variable Commented Aug 20, 2013 at 12:25
  • 1
    function increment has some syntax errors in it. Depending on what browser you are using, install Firebug for Firefox, or use Chrome tools, and use the console panel to check for errors. Commented Aug 20, 2013 at 12:25

2 Answers 2

2

Your JavaScript probably stops working because there are a few syntax errors. I've added tabs to make it better readable and have added comments to the code to point these errors out:

var Hunger = 8,
    interval = setInterval(increment, 10000);

function changeImage(a) {
    document.getElementById("img").src = a;
    window.setTimeout(goIdle, 20000)
}

// So far so good, but here it begins..
function goIdle() {
    document.getElementById("img").src = "idle.gif";

    function increment() {
        Hunger = Hunger % 24 + 1;
        // Why is this if-statement here?
        // You probably want to put this line above the previous line instead.
        if (Hunger >= 24)
    }
    // Missing the '{'?
    if (Hunger >= 12)
        changeImage("cry.gif")
// Because here are two '}' while there is only one open
}
}

// Because of these errors, this line will not be reached and thus
// there is no function eat()
function eat() {
    if (Hunger == 6) {
        changeImage("love.gif");
        // Remove 'var' here because otherwise you create a new variable
        // inside this function's closure.
        var Hunger = 0
    } else {
        ...
    }
}

These are easy to fix. If you want help with this, just leave a comment and I'll edit this answer.

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

5 Comments

you're not fixing the var Hunger = 0 problem
@C5H8NNaO4 Thanks for pointing that out, I was focused on the errors. I edited my answer.
You're welcome =) Yupp, i noticed it, +1. A little note, I would indent the first } after Hunger >= 12 by 4 spaces, it seems it should be the closing bracket for the if statement with the missing opening bracket. It would make it a bit more readable.
@C5H8NNaO4 True, but currently it's closing the one without indents, so I decided to put it there. Always using brackets would be even more readable, though.
Ok that makes sense too =)
0

Try with this:

I have changed the var Hunger to Hunger

function eat() {
    if (Hunger == 6) {
        changeImage("love.gif");
        Hunger=0;
    }
    else {
        ...
    }
}

1 Comment

Would you mind explaining what you changed in the code and why?

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.