1

I've been working for awhile on a huge mathematical multitool and I'm making a fraction simplifier in it (all with alerts). I tried the following, what did I do wrong?

if (confirm("Confirm to select a program\nCurrent program: Fraction Simplifier") == true) {
    var numerator = prompt("Enter the numerator");
    var denominator = prompt("Enter the denominator");
    var nFrac = numerator;
    var dFrac = denominator;
    simpFrac();
    alert(numerator + "/" + denominator + " simplified is " + nFrac + "/" + dFrac);
};
function simpFrac() {
    for (var i = 1; i < dFrac; i++) {
        fracV = fracV + 1;
        if (Number.isInteger(nFrac / fracV) && Number.isInteger(dFrac / fracV)) {
          nFrac = nFrac / fracV;
          dFrac = dFrac / fracV;  
        };
    };
};
001
  • 13,291
  • 5
  • 35
  • 66
Hyyped
  • 33
  • 4

2 Answers2

1

someone answered and deleted their answer, I implemented their method. It worked. Changed to

if (confirm("Confirm to select a program\nCurrent program: Fraction Simplifier") == true) {
    if (confirm("Confirm to select a program\nCurrent program: Fraction Simplifier") == true) {
    var numerator = prompt("Enter the numerator");
    var denominator = prompt("Enter the denominator");
    var GCD = (a, b) => b ? GCD(b, a % b) : a;
    let div = GCD(numerator, denominator);
    alert(numerator + "/" + denominator + " simplifies to " + numerator / div + "/" + denominator / div + ".");
Hyyped
  • 33
  • 4
0

Divide the numerator and the denominator by the GCD.

const GCD = (a, b) => b ? GCD(b, a % b) : a;
let numerator = 15, denominator = 18;
let div = GCD(numerator, denominator);
console.log(`${numerator}/${denominator} simplifies to ${numerator/div}/${denominator/div}`);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80