I have created a function that reduces two numbers using the relationship of fractions. The function is working perfectly, but there is an issue with it not returning the value as expected. I have tried different approaches such as declaring a new variable and then returning it, but still no luck. I even added console.logs to check if the function was stopping at a certain point, but unfortunately, it does not return anything.
Here is my code:
function reduceFraction(n, d) {
var numerator = n;
var denominator = d;
if (n % 7 === 0 && d % 7 === 0) {
numerator /= 7;
denominator /= 7;
console.log('Divided by 7');
reduceFraction(numerator, denominator);
} else {
if (n % 5 === 0 && d % 5 === 0) {
numerator /= 5;
denominator /= 5;
console.log('Divided by 5');
reduceFraction(numerator, denominator);
} else {
if (n % 3 === 0 && d % 3 === 0) {
numerator /= 3;
denominator /= 3;
console.log('Divided by 3');
reduceFraction(numerator, denominator);
} else {
if (n % 2 === 0 && d % 2 === 0) {
numerator /= 2;
denominator /= 2;
console.log('Divided by 2');
reduceFraction(numerator, denominator);
} else {
console.log('Was not divided by anything');
var reduced = numerator + "/" + denominator;
return reduced; //console.log(numerator + "/" + denominator); logs 1/18
}
}
}
}
}
reduceFraction(3, 54);
I am not sure if having nested if statements is causing any issues, but currently, this is the only way I can think of to reduce a fraction. Thank you in advance for your help.