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
Answer from Mark Elliot on Stack OverflowFor 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
I'm no expert in bitwise operators, but here's another way to get the whole number:
var num = ~~(a / b);
This will work properly for negative numbers as well, while Math.floor() will round in the wrong direction.
This seems correct as well:
var num = (a / b) >> 0;
Note: Only use ~~ as a substitution for Math.trunc() when you are confident that the range of input falls within the range of 32-bit integers.
Getting the Integer Portion from a Division - JavaScript - SitePoint Forums | Web Development & Design Community
How can I execute integer division and retrieve the remainder in JavaScript?
how did "Integer Division in Javascript" get so many upvotes? - Meta Stack Exchange
You can do integer division in JavaScript
Videos
I learned that you can do integer division in Python with //, for example 5.0 // 2 == 2.0 whereas 5.0 / 2 == 2.5. I wanted to know whether or not it was possible to do the same in JavaScript, and I found out that it was!
> 5 // 0.9 < 5 > 1 // 0.6 < 1 > 172 // 1 < 172
Try it yourself!
If you check out the timeline of this fairly interesting Programmers question on the Tanenbaum-Torvalds debates you'll notice that it exploded vote wise on March 22, when I posted it on Reddit.
Same with the UX question Do we need good-looking design for a program internal only to our company? which I posted on Reddit on April 16 (notice the spike in its timeline).
Obviously I can't be certain that all their traffic came from my Reddit posts, but I did get gold Publicist badges for both questions so at least 1000 unique visits on each are my fault ;P
But there aren't really any vote spikes in the integer division question, the most votes it got in a month were 9 in Nov 10, when it was first posted. Although I don't consider it a good question by any standard, it's age and noobesque quality probably account for its votes. Questions like this are a good reminder that votes are not always a sign of quality, as they can be so easily manipulated.
I think it is the phenomenon of herd and badges.
I see it a lot on Stack Overflow, when question, answer or comment gets high scoring or high view suddenly in a few seconds a lot of people also upvote/downvote/view this.
It's too weird that in a short period of time everybody thinks alike.
Another reason on voting up question is because of people who want to achieve badges like Civic Duty, Electorate, Sportsmanship, Suffrage and Vox Populi.