0

This code seems to loop through adding 1 to player1.score untill the score is === to whatever i put in the second if statement. Anyone know why?

pointScored: {
        startNextSet: function(Scorer) {
            if (gameController.bananasTaken < 3 && Scorer === "player1") {
                        console.log(gameController.player1.score);
                        gameController.player1.score += 1;
                    if (gameController.player1.score === 10 && 
                        gameController.bananasTaken === 0 &&
                        gameController.player1.bananaCount === 0) {
                            console.log(gameController.player1.score);
                            gameController.updatePlayerStats(gameController.Banana1, 20, gameController.canvas.height
                            - 20 - gameController.Banana1.height, gameController.player1, "left");
                            console.log("player 1's first point");


                    }

I'm currently learning about using a debugger but thought i'd leave this here to see if anyone knows why. Thanks.

user2602079
  • 1,283
  • 5
  • 20
  • 39

2 Answers2

2

There's a chance your values get evaluated as strings. The === operator doesn't do any type conversions, that's why its faster.

Consider changing your evaluation to use ==. The same issue has cropped up in another question.


I have refactored your code a bit & used the == notation I suggest above. Please try running it and tell me if it works.

pointScored:{
startNextSet: function(Scorer) {
    gc=gameController; //to save thy fingers from typing ache
if (gc.bananasTaken > 2 || Scorer !== "player1")
    return;

    console.log(gc.player1.score); // this logs 6 times from 0 to 5
    gc.player1.score += 1;
    if (gc.player1.score == 5 && gc.bananasTaken == 0) {
        alert(gc.player1.score); //*******!
        if(gc.player1.bananaCount == 0) {
            gc.updatePlayerStats(gc.Banana1, 20, gc.canvas.height - 20 - gc.Banana1.height, gc.player1, "left");
            console.log("player 1's first point");
        }
    }
}

}

Community
  • 1
  • 1
rath
  • 3,655
  • 1
  • 40
  • 53
  • Won't that make it fire _less_ than it should, not _more_? – Barmar Jul 30 '13 at 08:32
  • What do you mean? @Barmar – rath Jul 30 '13 at 08:33
  • Exact comparisons are more strict. So there are cases where `==` will match, but `===` will not. So why do you think `===` is causing the `if` to succeed when it shouldn't? – Barmar Jul 30 '13 at 08:35
  • 1
    In other words, what value could `gameController.player1.score` have that causes `if(gameController.player1.score === 5)` to succeed, but not `if(gameController.player1.score == 5)`? – Barmar Jul 30 '13 at 08:36
  • Because if the two types are different (one is string and the other int) it *shouldn't* fire, even if one value is `"5"` and the other is `5`. They must *both* be converted to the same type so as to compare `5==5`. Yes, explicitly taking care of conversion is a very good idea but **even if** you are using `==`. – rath Jul 30 '13 at 08:39
  • The problem he's reporting is that it DOES fire when it SHOULDN'T. You seem to be fixing the opposite problem. – Barmar Jul 30 '13 at 08:39
  • I think @Barmar is right, this would help if i wanted to make something fire that wouldn't fire. Thanks though – user2602079 Jul 30 '13 at 08:48
  • Apologies to both. I really have no idea *(I secretly hate JS)*. Give it a try though, just to be on the safe side. – rath Jul 30 '13 at 08:50
  • @rath thank you for refactoring my code, i only just saw. I will try that out now – user2602079 Jul 30 '13 at 09:45
  • @rath you missed the { and } for the first if. Did you want that if to end after return? – user2602079 Jul 30 '13 at 09:54
  • I was under the impression it was syntactically valid. Yes the first if is supposed to exit the function @user2602079 – rath Jul 30 '13 at 10:09
  • @rath Thank you! It does need the {} but i added them and it works. My code also works, but yours looks less complex so i will work at using that for my whole function - i want it to take care of when bananaCount is 1,2 and 3, for player 1 and 2. I think it will be easy enough. The reason my code and yours didn't work was it was being called contantly in a never ending loop. (whenever the ball was off the screen. I had to set up the ball on the screen again before calling the method. – user2602079 Jul 30 '13 at 10:18
1

As I look at your function, it seems that this logic needs to be INSIDE the gameController object.

jgroenen
  • 1,332
  • 1
  • 8
  • 13
  • Thanks. It probably should be for best practice, but why would it make it act differently? I don't see this solving the problem, but yes it would be good to do – user2602079 Jul 30 '13 at 08:43
  • For some reason i forgot, but pointScored is actually inside gameController. It's an object within gameController. That should be good practice yes? The main thing i would do to clean this code is change it to &&'s rather than ifs (i usually do), but that will still look fairly complex. – user2602079 Jul 30 '13 at 09:04
  • Thank you i will definitely check that out. I have solved the problem. My game is pong just by the way, and if the ball was past the goal, it would run the above code. I needed to reset the ball position before running the code so that it didn't keep running it in a never ending loop. – user2602079 Jul 30 '13 at 10:01