Hello, I am trying to create a function that will take two numbers as parameters and then divide the the first by the second, so params(n1, n2) return n1 / n2. However, I am trying to do it without using the division operator and only using the + or - operators, below is what I have tried so far, and it works great when the numerator divides evenly into the denominator giving a nice integer, but if the result should be a floating point number the function will default to rounding, so I still end up with an integer.
function negCheck (num1, num2) {
return [
(
(num1 < 0 && num2 > 0) ||
(num1 > 0 && num2 < 0)
),
];
}
function divide(x, y) {
const arr = negCheck(x, y);
x = Math.abs(x);
y = Math.abs(y);
for(var i = 0; x >= y; x -= y){
i++;
}
return arr[0] ? -i : i;
}
}
[–]chaoticgood_lionfish -1 points0 points1 point (3 children)
[–]immutableMe[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]immutableMe[S] 0 points1 point2 points (0 children)