【问题标题】:Not sure why syntax error ")" javascript不知道为什么语法错误“)”javascript
【发布时间】:2015-11-10 23:34:28
【问题描述】:

我正在尝试创建一个运行 while 循环的函数。

它应该运行以下内容:

  1. 当循环中的数字是3的倍数时,将参数string1附加到div
  2. 当循环中的数字是5的倍数时,将参数string2附加到div
  3. 否则,在下面附加另一个字符串

对于脚本标记中以“for (x % 3 == 0) {”开头的行,我收到 Chrome 工具箱错误“Uncaught SyntaxError: Unexpected token)”。不知道为什么……

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>

    <div id="output"></div>



    <script src="https://code.jquery.com/jquery-2.1.1.js"></script>
    <script>

    var this_that = function(string1, string2) {
      var x = 1
      while (x <= 100) {
        for (x % 3 == 0) {
        $(#output).append(x, string1)
      } else if (x % 5 == 0) {
        $(#output).append(x, string2)
      } else {
        $(#output).append(x,'is not a multiple of 3 or 5')
      }
      x++
    }
  }




</script>

【问题讨论】:

  • 应该是if (x % 3 == 0) for 的语法是for ([initialization]; [condition]; [final-expression]){ statement} 我强烈推荐你学习基础For Loop

标签: javascript jquery function for-loop while-loop


【解决方案1】:

您的for 应该是if

for (x % 3 == 0) {

应该是:

if (x % 3 == 0) {

【讨论】:

  • 不用担心。这发生在我们所有人身上……一直如此。没问题,很乐意提供帮助。
  • @AustinSefton,如果这个答案真的对你有帮助,那么请接受它作为一个可以接受的答案。我认为 Karl 应该得到它,也应该得到支持。
  • 感谢提醒 - 我是新来的
【解决方案2】:

这个for (x % 3 == 0)应该是这个if (x % 3 == 0)

您只需检查一个条件,如果 x 是 3 的倍数,并且您不想多次迭代一个语句块,这是 for 语句的目的。

至于你得到的错误,这是由于for语句的语法不正确造成的。正确的语法如下:

for ([initialization]; [condition]; [final-expression])
   statement

有关 for 语句的更多文档,请查看here

【讨论】:

    【解决方案3】:

    使用下面的

    var this_that = function(string1, string2) {
    var x = 1;
    while (x < 100) {
        if (x % 3 === 0) {
            $(#output).append(x, string1)
        } else if (x % 5 == 0) {
            $(#output).append(x, string2)
        } else {
            $(#output).append(x, 'is not a multiple of 3 or 5')
        }
        x++
    }
    }
    

    【讨论】:

      【解决方案4】:

      你为什么使用for

      for (x % 3 == 0) //problem lies here. It should be if(x % 3 == 0)
      {
              $(#output).append(x, string1)
      } 
      else if (x % 5 == 0) //if statement never started, then how can we have else if
      {
         $(#output).append(x, string2)
      } 
      else 
      {
         $(#output).append(x,'is not a multiple of 3 or 5')
      }
      

      【讨论】:

        【解决方案5】:

        如果您不想进行初始化和递增,则必须在条件之前和之后给出分号 for(;x%3==0;)

        其次,您使用 else 而没有 if 使用 if(x%3==0)

        希望对你有帮助!

        【讨论】:

          【解决方案6】:

          代码中的各种错误

          1. 您需要将选择器字符串包装为$('#output')
          2. 检查条件需要使用iffor 使用for循环

          var this_that = function(string1, string2) {
            var x = 1,
              op = $('#output');
            while (x <= 100) {
              if (x % 3 == 0) {
                op.append(x, ' is a multiple of 3 <br>');
              } else if (x % 5 == 0) {
                op.append(x, ' is a multiple of 5<br>');
              } else {
                op.append(x, ' is not a multiple of 3 or 5<br>');
              }
              x++;
            }
          }
          
          this_that();
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
          <div id="output"></div>

          【讨论】:

            【解决方案7】:

            除了其他答案之外,您的选择器上还缺少引号

            var this_that = function(string1, string2) {
                  var x = 1
                  if (x <= 100) {
                    for (x % 3 == 0) {
                    $('#output').append(x, string1)
                  } else if (x % 5 == 0) {
                    $('#output').append(x, string2)
                  } else {
                    $('#output').append(x,'is not a multiple of 3 or 5')
                  }
                  x++
                }
              }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-06-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多