I need to separate an integer into two numbers. Something like dividing by two but I only want integer components as a result, such as:
6 = 3 and 3
7 = 4 and 3
I tried the following, but I'm not sure its the best solution.
var number = 7;
var part1 = 0;
var part2 = 0;
if((number % 2) == 0) {
part1 = number / 2;
part2 = number / 2;
}
else {
part1 = parseInt((number / 2) + 1);
part2 = parseInt(number / 2);
}
This does what I want, but I don't think this code is clean.
Are there better ways of doing this?
parseInt
when you actually meanMath.floor
.javascript
tag questions are becoming incredibly simple (OP does zero research) or boring, the time of the day the question was posted, the mood of the people at that time... When I left a comment here it had just 6 upvotes, but in a short time span. Then, the question was listed in the "Hot network question". From that point on it was a snowball effect (or a positive feedback if you like).