未捕获到的SyntaxError:计算时出现意外标识符 [英] Uncaught SyntaxError: Unexpected identifier at compute

查看:71
本文介绍了未捕获到的SyntaxError:计算时出现意外标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些运行时返回 SyntaxError 的代码,但我不明白为什么.

I have some code that returns an Uncaught SyntaxError when I run it but I don't understand why.

我尝试将其通过JSHint进行,但无济于事.

I tried putting it through JSHint but to no avail.

以下代码显然是错误的:

Here is the code that is apparently wrong:

function compute(expr, x, string) {
  var whatisx = "x=" + toString(x) + ",";
  var tempAns = parseFloat(eval(whatisx + expr));
  var roundedAnswer = roundNumber(tempAns, 3);
  if (isNaN(tempAns) === true) {
    alert("error");
  }
  if (string) {
    return toString(roundedAnswer);
  } else if (!string) {
    return roundedAnswer;
  } else {
    return null;
    console.log("Error trying to compute value. The string value must be boolean.");
  }
}

运行它时,我没有任何控制台日志,并且说加号时出现错误:

When I run it, I don't get any console logs and it says there is an error at the plus sign in:

var tempAns = parseFloat(eval(whatisx + expr));

同一程序中的另一个问题也是SyntaxError,这是我的HTML.

Another problem in the same program that is also a SyntaxError is in my HTML.

这是我的html:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Grapher</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js" charset="utf-8"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/p5.js" charset="utf-8"></script>
    <script type="text/javascript" src="functions.js" charset="utf-8"></script>
    <script type="text/javascript" src="sketch.js" charset="utf-8"></script>
  </head>
  <body>
    <form>
      <label>Graph y=</label>
      <input id="mathExpr" type="text" name="mathExpr" value="">
      <label> from x=</label>
      <input id="x_min" type="text" name="x_min" value="">
      <label> to </label>
      <input id="x_max" type="text" name="x_max" value="">
      <input type="button" name="result" value="Result" onclick="compute(); runp5();">
    </form>
    <h2>Answer: <span id="output"></span></h2>
  </body>
</html>

对于这个,它说在这里有一个错误

For this one, it says there is an error at

<input type="button" name="result" value="Result" onclick="compute(); runp5();">

该如何解决这两个问题?预先感谢.

What can I do to fix both of those? Thanks in advance.

编辑:问题已解决.我在没有任何参数的情况下调用了compute.(感谢@Pointy)

Question resolved. I was calling compute without any parameters. (thanks @Pointy)

推荐答案

我刚才回到了这个问题,我意识到自己犯了另一个错误.这是我的解决方法:

I came back to this question just now, and I realized I was making another mistake. Here is my fix:

  1. 正如Pointy用户所注意到的那样,我在调用 compute()时没有提供必需的参数.

  1. As user Pointy noticed, I was calling compute() without its required parameters.

我应该在 return 之前的 else 语句中添加 console.log()全部:

I should add console.log() in my else statement before the return otherwise it won't get called at all:

// ...

else {
  console.log('message'); // this should come before return
  return null;
} 

这实际上并不能解决我详细说明的问题,但我想为以后的读者指出这一点.

This doesn't actually fix the problem I detailed, but I wanted to point it out for any future readers.

这篇关于未捕获到的SyntaxError:计算时出现意外标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆