1
var MDI = '39.741679085941385';
var revMDI = "";

I am needing to modify the MDI value by removing the "." and all characters to the right of it.

I've tried this code and I'm getting a mozilla error:

var revMDI = MDI.substr(MDI,0,indexOf(MDI,'.'));

The error I'm getting is:

org.mozilla.javascript.EcmaError: TypeError: Cannot find function substr.

Any help/direction would be appreciated. Thanks.

1
  • The first argument to substr() should be 0, not MDI. Commented Jun 23, 2014 at 20:10

6 Answers 6

4

In other words, get the integer value.

var revMDI = parseInt(MDI);
Sign up to request clarification or add additional context in comments.

Comments

2

You need to use MDI.substring(0,MDI.indexOf('.'));.

See the MDN Docs for substring and indexOf.

2 Comments

Why would the error say substr if the problem is with indexOf?
Sorry, I noticed the second problem first. I edited my answer to include both.
1

You can do it with just two characters:

var revMDI = MDI | 0;

The bitwise OR operator | coerces MDI to a number and truncates it to 39. And then the bitwise OR with 0 is the identity.

Comments

0

You could do it indexOf('.'),

var MDI = '39.741679085941385';
var revMDI = MDI.substr(0, MDI.indexOf('.'));

jsFiddle

3 Comments

Thanks for all the replies. Unfortunately, I'm getting the same error. I'm working in an IDE called Provenir and it apparently is pretty picky. Thanks.
@Melinda Did you try the fiddle?
This is actually not going through the browser but through the Provenir IDE, would that matter?
0

Your syntax is wrong:

revMDI = MDI.substr(0, MDI.indexOf('.'));

You have to declare the string you wish to modify with substr AND then with indexOf.

The way you were doing it kinda made no sense.

Comments

0

The correct syntax is:

var revMDI = MDI.substr(0, MDI.indexOf('.'));

But I would prefer Scimonster's answer more.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.