-5

The program is running an else statement when I'm expecting an if statement to run. When the program checks the gameObject's playerSelections property against the gameObject's colorSequence property I expect the program to log 'You got it. Way to go!' if the two arrays are equivalent. Instead I keep getting the console log statement 'Better luck next time!'. Does anyone have any ideas as to why this may be happening. `

const $headerText = $('.fly-in-text');
setTimeout(function() {
    $headerText.removeClass('temp-hide');
}, 500);

let gameObject = {
    colorIds: ['#blue', '#red', '#green', '#yellow'],
    currentLevel: 1,
    colorSequence: [],
    playerSelections: [],
    illuminate:function(color){
        setTimeout(function(){
            $(color).css('opacity', .5);
            setTimeout(function() {
                $(color).css('opacity', 1);
            },500);
        },500);
    },
    levelSequence: function(){
        const iterationCount = this.currentLevel + 2;
        for (let i = 0; i < iterationCount; i++) {
            this.colorSequence.push(this.colorIds[Math.floor(Math.random() * 4)]);
        }
        this.startGame(this.colorSequence);
    },
    startGame: function(sequence) {
        let i = 0;
        const self = this;
        var interval = setInterval(function(){
            self.illuminate(sequence[i]);
            i++;
            if (i >= sequence.length){
                clearInterval(interval);
            }
        }, 1000);
    }
}

$circle.click(function() {
    clearTimeout(interval);
    $(this).toggleClass('rotate');
    gameObject.levelSequence();

    $('.colors').click(function(){
        gameObject.playerSelections.push(`#${this.id}`);
        console.log(gameObject.playerSelections);
        checkForWin();
        // for (let i = 0; i < gameObject.colorSequence.length; i++) {
        //     playerSelections[i] = (`$${this.id}`);
        //     console.log(playerSelections)
        // }
    })
})
function checkForWin(){
    if (gameObject.playerSelections === gameObject.colorSequence){
        console.log('Comparing')
        if (gameObject.playerSelections === gameObject.colorSequence){
            console.log('You got it. Way to go!');
            gameObject.colorSequence = [];
            gameObject.playerSelections = [];
            gameObject.currentLevel += 1;
            $('.colors').off('click');
            return 'You got it. Way to go!';

        } else {
            gameObject.colorSequence = [];
            gameObject.playerSelections = [];
            $('.colors').off('click')
            console.log('Better luck next time!');
        }
    }
}
  • `gameObject.playerSelections` and `gameObject.colorSequence` are both arrays. They’re different arrays, so they will never be equal to one another. – Sebastian Simon May 18 '17 at 03:25
  • Comparing arrays like that?? See this: http://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript – Leandro Rodrigues May 18 '17 at 03:26
  • also check this out http://stackoverflow.com/questions/13757109/triple-equal-signs-return-false-for-arrays-in-javascript-why – Junbang Huang May 18 '17 at 03:26
  • Are you sure you are running the same code you posted above? Both of your `if` statements have identical condition expressions. So if the first one is `true`, then the second one must also be `true`. There is no way for the `else` to ever run. But your description says the `else` is running every time. That doesn't make sense. – RJM May 18 '17 at 03:31

1 Answers1

-1

Since playerSelections and colorSequence are arrays, your the condition gameObject.playerSelections === gameObject.colorSequence tests that the array references are equal, not the contents of the arrays.

You need to check that each element of the array is equal:

How to check identical array in most efficient way?

Community
  • 1
  • 1
david25272
  • 976
  • 6
  • 12