JSHINT为If语句方程式引发语法错误

时间:2018-10-17 18:33:43

标签: javascript jquery

我试图检查projectCount是否可被4除而不是被5除,然后在else if语句中反之亦然。我的代码编译了,但是jshint抛出15个语法错误。我还要如何构造它来消除那些语法错误?

jQuery(document).ready(function($) {
    if($(".projects li").hasClass("projectCols-30")) {
        var projectCount = $('.projectCols-30').length;

        if (projectCount % 4 = 0 && projectCount % 5 != 0) {
            alert("The total number of columns means there is always only 1 extra on a row");
        } else if (projectCount % 4 != 0 && projectCount % 5 = 0) {
            alert("The total number of columns means there is always only 2 extra on a row");
        }
    }
});
  

四个警告5错误的分配。 5预期条件表达式   而是看到一个作业。 7错误的分配。 7预期   条件表达式,而是看到一个赋值。

进行一些编辑后,以下内容不再引发语法错误 Talg123 ,请在注释中解决问题后将其发布为答案。

jQuery(document).ready(function($) {
    if($(".projects li").hasClass("projectCols-30")) {
        var projectCount = $('.projectCols-30').length;

        if (projectCount % 4 === 0 && projectCount % 5 !== 0) {
            alert("The total number of columns means there is always only 1 extra on a row");
        } else if (projectCount % 4 !== 0 && projectCount % 5 === 0) {
            alert("The total number of columns means there is always only 2 extra on a row");
        }
    }
});

2 个答案:

答案 0 :(得分:2)

不要使用单个=来比较数字,这是赋值运算符。 改写projectCount % 4 == 0projectCount % 4 === 0

答案 1 :(得分:0)

jQuery(document).ready(function($) {
if($(".projects li").hasClass("projectCols-30")) {
    var projectCount = $('.projectCols-30').length;

    if (projectCount % 4 === 0 && projectCount % 5 !== 0) {
        alert("The total number of columns means there is always only 1 extra on a row");
    } else if (projectCount % 4 !== 0 && projectCount % 5 === 0) {
        alert("The total number of columns means there is always only 2 extra on a row");
    }
}

});

基本上,您需要与之进行比较,这意味着您需要使用== \ ===来等于,而不是使用=

来设置变量