# Calculate the quotient and remainder of a division in JavaScript

Python's `divmod()` comes in handy quite often. Its purpose is to return a 2-tuple consisting of the quotient and remainder of a division. For example, `divmod(8, 3)` returns `(2, 2)` because `8 / 3 = 2` with a remainder of `2`.

In order to implement `divmod()` in JavaScript, we can use the built-in `Math.floor()` function to get the quotient and the modulo operator (`%`) to get the remainder of the division `x / y`.

```const divmod = (x, y) => [Math.floor(x / y), x % y];

divmod(8, 3); // [2, 2]
divmod(3, 8); // [0, 3]
divmod(5, 5); // [1, 0]```

## More like this

• JavaScript ·

### Calculate the nth root of a number in JavaScript

Use `Math.pow()` to calculate the nth root of a given number in JavaScript.

• JavaScript ·

### Calculate the powerset of a JavaScript array

Generate the powerset of a given array of numbers or other primitive values.

• JavaScript ·

### Find the prime factors of a number in JavaScript

Find the prime factors of a number using trial division in JavaScript.

• JavaScript ·

### Implement the SDBM hash function in JavaScript

SDBM is a simple, non-cryptographic hash function that can hash strings into whole numbers. Here's a JavaScript implementation.

Start typing a keyphrase to see matching snippets.