how do i create a decimal or a fraction by only using addition or subtract? I have the numbers 1 and 2, and I want to end up with .5 -- have been stuck on this for quite a bit! I cannot just do 1/2, I have to somehow use only addition or subtraction.
var divide = function(x, y) {
//the number of times you need to subtract y from x.
if (y === 0) {
return 0
}
// if
if (x - y === 0) {
return 1;
}
if (x < y) {
return 0; <--- this is where the problem is
} else {
return (1 + divide(x - y, y)); // need to get this toFixed somehow.
}
};
If you're restricted to integers, then you can't.
The integers are closed under addition. (In other words, adding two integers gives you another integer.) Same for subtraction.