views:

50

answers:

5

Hi there

I'm working on a easter egg, where you have to activate some links in the correct order, before the secret is revealed.

I can't get this script to work. I guess I've wrote something wrong, but can't see what it is...

<script>
    $(document).ready(function() {
        $('#show').hide();
        var StepOfThree = 0;
        alert(StepOfThree);

        $('#linkone').mouseover(function() {
            StepOfThree = 1;
            alert(StepOfThree);
        });

        $('#linktwo').mouseover(function() {
            if (StepOfThree1 === 1) {
                StepOfThree = 2;
                alert(StepOfThree);
            } else {
                StepOfThree = 0;
                alert(StepOfThree);
            }
        });

        $('#linkthree').mouseover(function() {
            if (StepOfThree1 === 2) {
                $('#show').show();
                alert(StepOfThree);

            } else {
                StepOfThree = 0;
                alert(StepOfThree);
            }
        });
    });
</script>

    <a href="#" id="linkone">Link #1</a>
    <a href="#" id="linktwo">Link #2</a>
    <a href="#" id="linkthree">Link #3</a>

    <div id="show">This is hidden content</div>

The mouseOver on the #linkTwo and #linkThree doesn't even give me an Alert.. What have I done wrong?

A: 

Why not use the built in operators to check equivalence? The is method is meant to test selectors on jQuery objects, not to test equivalence on regular objects or variables.

Instead of:

if ($(StepOfThree).is(1))

Use:

if (StepOfThree === 1) {

CD Sanchez
I've updated it, but link#2 and link#3 still doesn't work... Can you explain why?
Kenneth B
+1  A: 
Matt Ball
Your absolutely right... :-$
Kenneth B
I've uploaded the code - can you please look at it again?
Kenneth B
@Bears: I don't think that's how closures in javascript work. It will only "seal" the value as you say if it's passed as an argument to a function, since that's the only way to create a new lexical scope in javascript. It still works fine without storing it as an object property: http://jsbin.com/iluse3
CD Sanchez
@Daniel: I haven't looked at your code, but you're definitely wrong about having to pass in the value - that's the whole difference between a closure and a "regular" function.
Matt Ball
@Bears will eat you: If that were true then the module pattern would not work. Functions have access to the variables (not values) and parameters of the function they are defined within -- this is verbatim from Douglas Crawford. The reason why the OP's script does not work is because "StepOfThree1" is not defined. Read the "Creating closures in loops: A common mistake" section in your link to see what I mean when I said it would not "seal" the value. You can only seal values by creating a function factory which overwrites the closure environment with its own variables.
CD Sanchez
@Daniel: you're absolutely right. Sorry! I hadn't even noticed that the OP was using a variable called `StepOfThree1`. I'm going to poach your JSBin link and clean up my answer. That's what I get for answering at the end of a long day. Thanks.
Matt Ball
A: 

You need to remove your vars

<script>
    $(document).ready(function() {
        $('#show').hide();
        var StepOfThree = 0;

        $('#linkone').mouseover(function() {
            StepOfThree = 1;
        });

        $('#linktwo').mouseover(function() {
            if (StepOfThree == 1) {
                StepOfThree = 2;
            } else {
                StepOfThree = 0;
            }
        });

        $('#linkthree').mouseover(function() {
            if (StepOfThree == 2 ) {
                $('#show').show();
            } else {
                StepOfThree = 0;
            }
        });
    });
</script>

You are including jquery right?

Capt Otis
A: 

You have redeclared your variable, overwriting its scope.

    ...
    var StepOfThree = 0; 

    $('#linkone').mouseover(function() { 
        var StepOfThree = 1; 
    });
    ...

There are two variables there, both named 'StepOfThree'. To fix, take away the 'var' for all but the first declaration.

Matt Brunell
I have updated the code... Can you please look again? Because it doesn't work with LInk2 and 3...
Kenneth B
Variable named "StepOfThree1"? Use the original name.
Matt Brunell
A: 

What's happening is that your StepOfThree variable is not global - it is inside your $(document).ready function. Declare it outside: script open tag var StepOfThree=0; $(document).ready stuff

electrichead
Nope. It's more subtle than that.
Matt Ball