简单比较中的Javascript错误

时间:2015-09-22 00:13:24

标签: javascript

这真的很奇怪!我的代码中有两个" if" s用于比较两个值

if(block>currblock)
{
    currblock++;
    console.log("currblock:"+currblock+" | block:"+block);
}
if(block<currblock)
{
    currblock--;
    console.log("currblock:"+currblock+" | block:"+block);
}

当代码运行时,它确实是错误的。例如,我用值block = 13和currblock = 9测试它。当我第二次触发代码时,如果&#34;执行!

我使用了控制台日志来确保值是否正确。控制台输出正确。

任何想法?

3 个答案:

答案 0 :(得分:0)

  自从屏幕截图发布后

无关紧要

看来你的javascript被“解释为好像你写了

if(block>currblock);
{
    currblock++;
    console.log("1. currblock:"+currblock+" | block:"+block);
}
if(block<currblock);
{
    currblock--;
    console.log("2. currblock:"+currblock+" | block:"+block);
}

完全有效,但不是你想要的。是服务器上的东西添加;不应该在哪里?查看您的页面源代码,或尝试重新格式化您的代码,看起来像这样 - 无论如何,您应该使用javascript,因为可选的诅咒;

if(block>currblock) { // <=== move this { to the if line
    currblock++;
    console.log("1. currblock:"+currblock+" | block:"+block);
}
if(block<currblock) { // <=== move this { to the if line
    currblock--;
    console.log("2. currblock:"+currblock+" | block:"+block);
}

答案 1 :(得分:0)

尝试在ifs之前解析。像这样:

if(parseInt(block)>parseInt(currblock)) { 
    currblock++;
    console.log("1. currblock:"+currblock+" | block:"+block);
}
if(parseInt(block)<parseInt(currblock)) { 
    currblock--;
    console.log("2. currblock:"+currblock+" | block:"+block);
}

答案 2 :(得分:0)

你试过这个吗?

    var block = 13
    var currblock = 9;
    if(block>currblock){
        currblock++;
        console.log("currblock:"+currblock+" | block:"+block);
    }
    else if(block<currblock){
        currblock--;
        console.log("currblock:"+currblock+" | block:"+block);
    }