简体   繁体   English

如何修复此语法错误?

[英]How do I fix this syntax error?

The code is: 代码是:

  function roundAmount(theDecimal) { 
    var s = "" + Math.round(theDecimal * 100) / 100 
    var i = s.indexOf('.') 
    if (i < 0) { 
        return s + ".00" 
    } 
    var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3) 
    if (i + 2 == s.length)     
        t += "0" 
    return t 
  }

The line with the error: 带错误的行:

if (i < 0) return s + ".00"

The error is: 错误是:

error: expected (;)

does anyone know how to fix this? 有谁知道如何解决这一问题?

if (i < 0) return s + ".00";

Note the ; 注意; at the end of the statement. 在声明的最后。 Personally, I prefer surrounding almost all my if s in {} such as 就个人而言,我更喜欢围绕{}几乎所有的if

if (i < 0) 
{
    return s + ".00";
}

Which helps in debugging and stepping though code. 这有助于调试和步进代码。

About your script: 关于你的脚本:

The problem in the script above is that last if statement which does some operations followed by a return. 上面脚本中的问题是最后一个if语句执行一些操作后跟一个返回。 You need a semi-colon after the operation. 手术后需要分号。

In the future, as good practice, make sure to put a semi-colon after every valid statement. 在将来,作为良好的做法,请确保在每个有效的陈述后放置一个分号。 That way this won't bother you. 这样这不会打扰你。

Think of each line as a thought, and curly braces as ways to "group" and "relate" thoughts together. 将每一行想象成一种思想,并将花括号视为将“分组”和“联系”思想结合在一起的方式。

The below is a full thought that says "give me a variable "i" and give it the value 1 + 2; 下面是一个完整的想法,说“给我一个变量”i“并给它1 + 2的值;

var i = 1 + 2;

The below is a full thought about a condition that says "If i is 3 then add 1 to i". 下面是对“如果我是3然后加1”的条件的完整考虑。 The thought "add 1 to i" is its own thought, so it needs a semicolon. “为我添加1”的想法是它自己的想法,所以它需要一个分号。 Since the curlybraces for the IF statement are special in that they don't need a semi-colon after their "full thought" as long as you put a "block" (which is what curlybraces really make) after it, to enclose the thought. 因为IF语句的花括号是特殊的,因为只要你在它之后加上一个“块”(这就是花括号的真正构成),它们就不需要在它们“完全思考”之后使用分号,以包含思想。

This means the following is valid: 这意味着以下内容有效:

if( i == 3 ) {
    i = i + 1;
}

The following is not valid because the semi-colon after the if ends the "thought" before the if knows what to do if i equals 3: 因为之后的分号如果结束之前,如果知道该怎么做,如果我等于3的“思想”下面是无效

if( i == 3 ) ; {
    i = i + 1;
}

For a basic JavaScript tutorial, check out W3Schools . 有关基本的JavaScript教程,请查看W3Schools

"There must be a better way?" “一定会有更好的办法?”

Any time you find yourself doing a lot of string operations on decmials, it's a good idea to ask yourself "is there a better way to do this?". 每当你发现自己在decmials上做了大量的字符串操作时,最好问自己“有没有更好的方法来做到这一点?”。

It looks like you're writing a function to round a number to the nearest hundredths while displaying two decimal points. 看起来你正在编写一个函数来将数字四舍五入到最接近的百分数,同时显示两个小数点。 There's a much easier way to do this. 有一个更简单的方法来做到这一点。 You can just round to the nearest hundredths and have javascript output the fixed point number. 你可以四舍五入到最接近的百分之一,并有javascript输出固定点数。

Example: 例:

function roundAmount( theDecimal ) {
    //first round to the nearest hundredth
    //then return the value with two decimal places as a string
    return theDecimal.toFixed( 2 );
}

Add a semicolon at the end of the line! 在行尾添加分号! Like this: 像这样:

if (i < 0) return s + ".00";

It's expecting a semicolon, so add a semicolon. 它期待一个分号,所以添加一个分号。

if (i < 0) 
  return s + ".00";

I don't see anything particularly wrong with the code you posted in the earlier comments, but may I suggest always adding a semi-colon to the end of your statements. 我没有看到您在之前的评论中发布的代码有任何特别的错误,但我可以建议您在语句的末尾添加一个分号。 Though they are not required, it usually makes it easier to debug problems such as these. 虽然它们不是必需的,但它通常可以更容易地调试这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM