views:

249

answers:

5

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?

+4  A: 

Add a semicolon at the end of the line! Like this:

if (i < 0) return s + ".00";
Martin Olsen
+4  A: 

It's expecting a semicolon, so add a semicolon.

if (i < 0) 
  return s + ".00";
Brandon
+5  A: 
if (i < 0) return s + ".00";

Note the ; at the end of the statement. Personally, I prefer surrounding almost all my ifs in {} such as

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

Which helps in debugging and stepping though code.

Erich Mirabal
A: 

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.

darthnosaj
+4  A: 

About your script:

The problem in the script above is that last if statement which does some operations followed by a return. 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;

var i = 1 + 2;

The below is a full thought about a condition that says "If i is 3 then add 1 to i". The thought "add 1 to i" is its own thought, so it needs a semicolon. 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.

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:

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

For a basic javascript tutorial, check out the w3cschools reference: - http://www.w3schools.com/js/js_intro.asp

"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?".

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.

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 );
}
coderjoe
+1. Nice answer.
Erich Mirabal
that did it thanks