【问题标题】:Javascript program is not recognizing an if statement [duplicate]Javascript程序无法识别if语句[重复]
【发布时间】:2017-05-18 03:18:25
【问题描述】:

当我期望 if 语句运行时,程序正在运行 else 语句。当程序根据游戏对象的 colorSequence 属性检查游戏对象的 playerSelections 属性时,我希望程序记录“你知道了。好样的!如果两个数组是等价的。相反,我不断收到控制台日志语句“下次祝你好运!”。有没有人知道为什么会发生这种情况。 `

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 和 gameObject.colorSequence 都是数组。它们是不同的数组,因此它们永远不会彼此相等。
  • 这样比较数组??看到这个:stackoverflow.com/questions/7837456/…
  • 您确定您运行的代码与您在上面发布的代码相同吗?您的两个 if 语句具有相同的条件表达式。所以如果第一个是true,那么第二个也必须是true。 else 永远无法运行。但是您的描述说else 每次都在运行。这没有意义。

标签: javascript if-statement


【解决方案1】:

由于playerSelections 和colorSequence 是数组,您的条件gameObject.playerSelections === gameObject.colorSequence 测试数组引用 是否相等,而不是数组的内容。

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

How to check identical array in most efficient way?

【讨论】:

  • 谢谢。这个解决方案效果很好
猜你喜欢
  • 1970-01-01
  • 2017-07-10
  • 2017-09-05
  • 2020-02-15
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 2020-02-25
  • 1970-01-01
相关资源
最近更新 更多