I am trying to make a Fraction Calculator in HTML Javascript, I need to simplify the fraction how can I do this?

Ethang70

The non script part of my calculator is this (With all the inputs):

<body>
    <p class="box">Fraction Calculator</p>
    <input type="number" id="n1">
    <select id="operation">
        <option value="1">X</option>
        <option value="2">/</option>
        <option value="3">+</option>
        <option value="4">-</option>
    </select>
    <input type="number" id="n2">
    <br>
    <input type="number" id="n3">
    <input type="number" id="n4" style="margin-left:37px">
    <br>
    <button style="margin-left:130px"; onClick="document.getElementById('fraction').innerHTML = option()">Go!</button>
    <p id="fraction"></p>
</body>

And my option function is this:

function option()
 {
 selection = document.getElementById('operation').value;


 if(selection == "1"){

 a = multiply();
 return a
 }
 else if(selection == "2"){
 b = division();
 return b
 }
 else if(selection == "3"){
 c = addition();
 return c
 }
 else{
 d = subtraction();
 return d
 }
 }

So my current code for my multiplication part fo my calculator is this:

 function multiply()
 {
 Number1 = parseInt(document.getElementById("n1").value);
 Number2 = parseInt(document.getElementById("n2").value);
 Number3 = parseInt(document.getElementById("n3").value);
 Number4 = parseInt(document.getElementById("n4").value);


 var topm = Number1 * Number2;
 var botm = Number3 * Number4;
 return topm + "/" + botm; }

So currently my calculator works but none of the fractions are simplified. How would I be able to simplify these fractions? (I am decently new to code) Thanks :) Here is the JSFiddle link: https://jsfiddle.net/Ethang70/npgL3gd9/

Zyberzero

To simplify fractions, what you do is that you calculate the greatest common divisor between the denominator and numerator, and divide both with the GCD.

One way to calculate the GCD is to use the Eucledian algorithm.

One implementation in Javascript could be:

 function gcd(a,b)
 { 
   if (b == 0)
   {
     return a;
   }
   else
   {
     return gcd(b, a % b);
   }
}

And thereafter, when you print your fraction you can do something like this:

function multiply()
{
  Number1 = parseInt(document.getElementById("n1").value);
  Number2 = parseInt(document.getElementById("n2").value);
  Number3 = parseInt(document.getElementById("n3").value);
  Number4 = parseInt(document.getElementById("n4").value);

  var topm = Number1 * Number2;
  var botm = Number3 * Number4;
  var divisor = gcd(topm, botm);
  topm = topm / divisor;
  botm = botm / divisor;
  return topm + "/" + botm;
}

Good luck!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert "fraction of day" to Java time?

how can I apply fixed fraction to the integer

How would I make a new fraction if a fraction cannot be simplified?

How can I improve the rotation of fraction labels in a pyplot pie chart

how can I convert a fraction inputted as string to float in C

How can i make a calculator in SQL?

How do I transform a fraction in javascript?

How can i change the previous fraction value when the fraction value is 5. in c language

How can I stringify a fraction with N decimals in C++

How do i plot this polynomial fraction using numpy and matplotlib?

i just started learning javascript and i'm trying to make a calculator website but can't get it to work

how to get fraction as it is ( without getting itssimplest fraction )? i vainly tried using this:

Why can't I specify the xycoords='axes fraction'?

How can I simplify this?

How can I make this faster? Do I need dplyr?

How do I find the reciprocal of a fraction in C?

How can I make this calculator work

How do i increment by the fraction required and return as vector

How do I convert an improper fraction to a mixed number in Java?

I am trying to make a calculator, a simple one

I am new to javascript, and I am trying to make a html program to calculate dog age

I can't do a simple calculator with javascript

How do I make this more efficient- Consider the fraction, n/d, where n and d are positive integers

How to simplify a fraction efficiently?

How do I make a day of the week calculator?

How I can simplify this?

How can I convert a fraction expression into a character vector in R?

I am trying to make a simple calculator without eval() and whole equations

How can I get rid of fraction? (The fraction somehow still remains.)