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.

  • 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? – Obsidian Age Aug 04 '17 at 03:29
  • You should use `if (winner() == true)` instead of `if (winner == true)` – huydq5000 Aug 04 '17 at 03: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. – jdmdevdotnet Aug 04 '17 at 03:30
  • as well as what @huydq5000 pointed out. – jdmdevdotnet Aug 04 '17 at 03:32
  • Technically *either* `if (winner() == true)` or `if (winner == true)` would work. Though there are [**slight differences**](https://stackoverflow.com/questions/3246928/in-javascript-does-it-make-a-difference-if-i-call-a-function-with-parentheses). – Obsidian Age Aug 04 '17 at 03:35
  • Nevertheless, his main issue (what I believe, not enough code/information to make sure) is scoping with `wygrany` variable. – jdmdevdotnet Aug 04 '17 at 03:37
  • @ObsidianAge: In this case, winner() and winner is totally different. winner will return the function definition, winner() will execute the function and return the value. – huydq5000 Aug 04 '17 at 03:41
  • @ObsidianAge these are the only 3 cases i have. I see – Adrian Solarczyk Aug 04 '17 at 03:48

2 Answers2

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.

jdmdevdotnet
  • 1
  • 2
  • 19
  • 50
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?

  • 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. – jdmdevdotnet Aug 04 '17 at 04:01