Javascript程序无法识别if语句

Javascript程序无法识别if语句,javascript,if-statement,Javascript,If Statement,当我希望运行if语句时,程序正在运行else语句。当程序检查游戏对象的playerSelections属性和游戏对象的colorSequence属性时,我希望程序会记录“You got it”。干得好如果两个数组相等。相反,我一直得到控制台日志语句“祝你下次好运!”。有人知道为什么会发生这种情况吗` const $headerText = $('.fly-in-text'); setTimeout(function() { $headerText.removeClass('temp-hi

当我希望运行if语句时,程序正在运行else语句。当程序检查游戏对象的playerSelections属性和游戏对象的colorSequence属性时,我希望程序会记录“You got it”。干得好如果两个数组相等。相反,我一直得到控制台日志语句“祝你下次好运!”。有人知道为什么会发生这种情况吗`

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!');
        }
    }
}
const$headerText=$('.fly-in-text');
setTimeout(函数(){
$headerText.removeClass('temp-hide');
}, 500);
让游戏对象={
颜色ID:['#蓝色'、'#红色'、'#绿色'、'#黄色'],
当前级别:1,
颜色序列:[],
玩家选举:[],
照明:功能(颜色){
setTimeout(函数(){
$(颜色).css('opacity',.5);
setTimeout(函数(){
$(颜色).css('opacity',1);
},500);
},500);
},
levelSequence:function(){
const iterationCount=this.currentLevel+2;
for(设i=0;i=序列长度){
间隔时间;
}
}, 1000);
}
}
$circle.单击(函数(){
清除超时(间隔);
$(this.toggleClass('rotate');
gameObject.levelSequence();
$('.colors')。单击(函数(){
gameObject.playerSelections.push(`${this.id}`);
log(gameObject.playerSelections);
checkForWin();
//for(设i=0;i
由于
playerSelections
colorSequence
是数组,因此您的条件
gameObject.playerSelections===gameObject.colorSequence
测试数组引用是否相等,而不是数组的内容

您需要检查数组的每个元素是否相等:


gameObject.playerSelections
gameObject.colorSequence
都是数组。它们是不同的数组,所以它们永远不会彼此相等。比较这样的数组??看看这个:也看看这个,你确定你运行的代码与上面发布的相同吗?两个
if
语句具有相同的条件表达式。因此,如果第一个是
true
,那么第二个也必须是
true
else
无法运行。但是您的描述说,
else
每次都在运行。那没有道理,谢谢你。这个解决方案非常有效