0
function winners() {
updating = true;
if (mm == "Best of 3") {
    var wygrany = (s1 == "2")? 'left' : 'right';
    return true;
}
if (mm == "Best of 5") {
    var wygrany = (s1 == "3")? 'left' : 'right';
    return true;
}
if (mm == "Best of 7") {
    var wygrany = (s1 == "4")? 'left' : 'right';
    return true;
}
return false; }

This is the code that should be deciding if my var wygrany = 'left' or 'right'.

function runUpdate() {
if (timeOld == timeNew) return;

if (winners == true) {
        updating = true;
            setTimeout(function(){
                    $('.team.center .name').set('$', '-flipInY +fadeOut');
                    if(wygrany == "left") {
                        $('.team.right').set('$', '+animated +fadeOutDown');
                        $('.team.left').set('$', '+winner_show');
                        $('#ww').set('$', '-hidden +fadeIn');
                        $('.bg_winner').set('$', '-hidden +fadeIn');
                    } else {
                        $('.team.left').set('$', '+animated +fadeOutUp');
                        $('.team.right').set('$', '+winner_show');
                        $('#ww').set('$', '-hidden +fadeIn');
                        $('.bg_winner').set('$', '-hidden +fadeIn');
                    }
            updating = false;
        }, 1000);
}

This is the part responsible for display. Although code is not working, my function winners always returns 'true' and then script stops. It is probably syntax error but i can't find it.

8
  • Currently your function will returns true if mm is equal to Best of 3, Best of 5 or Best of 7. It will return false for other use cases. Are these the only use cases you have? Were you intending for your variable wygrany to affect the return? Commented Aug 4, 2017 at 3:29
  • You should use if (winner() == true) instead of if (winner == true) Commented Aug 4, 2017 at 3:30
  • @ObsidianAge Yeah I was going to ask about mm but he's trying access a variable that's not defined globally in it's own scope. Which is numero uno problem. Commented Aug 4, 2017 at 3:30
  • as well as what @huydq5000 pointed out. Commented Aug 4, 2017 at 3:32
  • Technically either if (winner() == true) or if (winner == true) would work. Though there are slight differences. Commented Aug 4, 2017 at 3:35

2 Answers 2

1

You have a scope issue. You're declaring var wygrany inside of a function scope, which any scope that doesn't reside inside that one does not have access.

//outside scope
var wygrany = '';

function winners() {
updating = true;

if (mm == "Best of 3") {
    wygrany = (s1 == "2")? 'left' : 'right';
    return true;
}
if (mm == "Best of 5") {
    wygrany = (s1 == "3")? 'left' : 'right';
    return true;
}
if (mm == "Best of 7") {
    wygrany = (s1 == "4")? 'left' : 'right';
    return true;
}
return false; }

And then you can access it anywhere. Also you need to do:

if (winners() == true) {

Not sure if winners is a variable too? Confusing how you have it, but if that's what your intent was, you have to add the () to make the function call.

I don't know why it would "always return true" seems like the function isn't even getting called though since you don't invoke the function winners(). But fix these issues and if you're still having trouble we can see why it's always returning true.

Check out developer tools, you would have seen these errors in console debugger.

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

Comments

0

Actually i needed to redefine my function winners a little bit:

function winners() {
updating = true;
if (mm == "Best of 3") {
    if(s1 == "2" || s2 == "2") {
        wygrany = (s1 == "2")? 'left' : 'right';
        return true;
    } else {
        return false;
    }
}
if (mm == "Best of 5") {
    if(s1 == "3" || s2 == "3") {
        wygrany = (s1 == "3")? 'left' : 'right';
        return true;
    } else {
        return false;
    }
}
if (mm == "Best of 7") {
    if(s1 == "4" || s2 == "4") {
        wygrany = (s1 == "4")? 'left' : 'right';
        return true;
    } else {
        return false;
    }
} }

I declared variable wygrany outside scope. But if i change

if (winners == true)

to

if (winners() == true)

script stops even earlier. I forgot to add winners is variable which i declared earlier and forgot to remove, could that affect the code?

1 Comment

Did you do any debugging? I have no idea why "script stops even ealier". I just told you what you had wrong with what you gave us. Check the console and let me know if you see an error, so I can assist further. So far it seems like I fixed two of the issues, I'm sure we can find the other(s). Or put all of this on a fiddle so we can reproduce. Also you shouldn't put this as an answer if it doesn't fix your problem.

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.