Forums > Javascript

### modulo operations in an if statement

Jul 31 2008 | 2:52 pm

Hi,

I’m totally new to Javascript so sorry if this problem is a bit lame, but I have the following question:

I am trying to do some kind of modulo operation in an if statement, but am I correct to note that there is not a predefined modulo operator available in Javascript’s Math object?

How would you do it then? To do something like this in C (if it hadn’t a modulo operator that is ofcourse) would be to explicitly cast the 2 operands to integers and then divide them by each other, then multiply the result with the number you divided by and then substract this from the first term.

Like for instance: 27%6 =
(int)27 / (int)6 = 4,
4 * 6 = 24,
27 – 24 = 3.

Is something simmilar possible in Javascript?

Jul 31 2008 | 3:01 pm

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Operators:Arithmetic_Operators

Modulo is an arithmetic operator, not part of the Math object. Same as the
C/C++ operator: %.

— Paul

On Thu, Jul 31, 2008 at 9:52 AM, Michael Dzjaparidze <
m_dzjaparidze@hotmail.com> wrote:

>
> Hi,
>
> I’m totally new to Javascript so sorry if this problem is a bit lame, but I
> have the following question:
>
> I am trying to do some kind of modulo operation in an if statement, but am
> I correct to note that there is not a predefined modulo operator available
> in Javascript’s Math object?
>
> How would you do it then? To do something like this in C (if it hadn’t a
> modulo operator that is ofcourse) would be to explicitly cast the 2 operands
> to integers and then divide them by each other, then multiply the result
> with the number you divided by and then substract this from the first term.
>
> Like for instance: 27%6 =
> (int)27 / (int)6 = 4,
> 4 * 6 = 24,
> 27 – 24 = 3.
>
> Is something simmilar possible in Javascript?
>

—– |(*,+,#,=)(#,=,*,+)(=,#,+,*)(+,*,=,#)| —–

Jul 31 2008 | 3:07 pm

Yes, i see now, how stupid of me… thanks for pointing it out and thanks for the link

Viewing 3 posts - 1 through 3 (of 3 total)

Forums > Javascript