HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptCritical

How to perform an integer division, and separately get the remainder, in JavaScript

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howremainderandintegertheseparatelygetdivisionjavascriptperform

Problem

In JavaScript, how do I get:

  • The whole number of times a given integer goes into another?



  • The remainder?

Solution

For some number y and some divisor x compute the quotient (quotient)[1] and remainder (remainder) as:

const quotient = Math.floor(y/x);
const remainder = y % x;


Example:

const quotient = Math.floor(13/3); // => 4 => the times 3 fits into 13  
const remainder = 13 % 3;          // => 1


[1] The integer number resulting from the division of one number by another

Code Snippets

const quotient = Math.floor(y/x);
const remainder = y % x;
const quotient = Math.floor(13/3); // => 4 => the times 3 fits into 13  
const remainder = 13 % 3;          // => 1

Context

Stack Overflow Q#4228356, score: 1904

Revisions (0)

No revisions yet.