-2

I have something like this

  function myFunction1() {
var aEquation = prompt("What is a? (if it is not there write 1)");
var bEquation = prompt("What is b?");
var cEquation = prompt("What is C?");
  var x10 = prompt("What is your first X coordinate?");
  var x20 = prompt("What is your second X coordinate?");
  var x30 = prompt("What is your thrid X coordinate?");
  var x40 = prompt("What is your fourth X coordinate?");
  var x50 = prompt("What is your fifth X coordinate?");
  var x60 = prompt("What is your sixth X coordinate?");
  var x70 = prompt("What is your seventh X coordinate?");
  var x80 = prompt("What is your eighth X coordinate?");
  var x90 = prompt("What is your ninth X coordinate?");
  document.getElementById("demo").innerHTML = "Your equation is:" + " " + aEquation + "x^2" + " " + "+" + " " + bEquation + "x" + " " + "+" + " " + cEquation;
  document.getElementById("Answerone").innerHTML = (eval(x10)*eval(x10)*aEquation)+(bEquation*eval(x10)*1)+(cEquation*1);
  document.getElementById("Answertwo").innerHTML = (eval(x20)*eval(x20)*aEquation)+(bEquation*eval(x20)*1)+(cEquation*1);
  document.getElementById("Answerthree").innerHTML = (eval(x30)*eval(x30)*aEquation)+(bEquation*eval(x30)*1)+(cEquation*1);
  document.getElementById("Answerfour").innerHTML = (eval(x40)*eval(x40)*aEquation)+(bEquation*eval(x40)*1)+(cEquation*1);
  document.getElementById("Answerfive").innerHTML = (eval(x50)*eval(x50)*aEquation)+(bEquation*eval(x50)*1)+(cEquation*1);
  document.getElementById("Answersix").innerHTML = (eval(x60)*eval(x60)*aEquation)+(bEquation*eval(x60)*1)+(cEquation*1);
  document.getElementById("Answerseven").innerHTML = (eval(x70)*eval(x70)*aEquation)+(bEquation*eval(x70)*1)+(cEquation*1);
  document.getElementById("Answereight").innerHTML = (eval(x80)*eval(x80)*aEquation)+(bEquation*eval(x80)*1)+(cEquation*1);
  document.getElementById("Answernine").innerHTML = (eval(x90)*eval(x90)*aEquation)+(bEquation*eval(x90)*1)+(cEquation*1); 
}

I know I can use functions but I'm doing it this way right now. Currently, if I put a number like 3/2 in for x10, x20, etc, the answer will come out correct but in a decimal form like 0.5, 1.75, etc. Is there any way to make them all come out as improper fractions? Instead of 0.5, 1.75, I want it to display 1/2, 7/4 respectively. Thanks!!

Leo L.
  • 145
  • 2
  • 2
  • 9
  • BTW, I wrote this so I can answer my Kumon questions more quickly xD – Leo L. Jun 25 '17 at 06:38
  • 1
    Possible duplicate of [Convert a decimal number to a fraction / rational number](https://stackoverflow.com/questions/14783869/convert-a-decimal-number-to-a-fraction-rational-number) – JJJ Jun 25 '17 at 06:40

1 Answers1

0

What you're asking is possible for non recurring fractions of course.

Take the numerator and the denominator separately- like 1.75 as 175 and 100, then find the gcd of these two numbers.

Then divide both the numbers by the gcd and format the new numerator and denominator on your own in the a/b form.

Hope this helps !

EDIT:

You can write a small script like this. U have to do this for every number. NOTE: THIS DOESNT GIVE CORRECT RESULTS FOR RECURRING DECIMALS.

<script>
function myFunction() {
    convert(".00033")
}

function gcd( a,  b)
{
    var r = a%b;
    while(r != 0)
    {
        a = b;
        b = r;
        r = a%b;
    }
    return b;
}

function convert( inp)
{
    var a="", b = 1, flag = 0;
    for( i = 0; i<inp.length; i++)
    {
        if(flag)
        {
            console.log("bing");
            b = b*10;
        }
        if(inp.charAt(i) != '.')
        {
            a += inp.charAt(i)
        }
        else
        {
            flag = 1
        }


    }
    a = parseInt(a);
    console.log(a);
    console.log(b);
    var x = gcd(a, b);
    console.log(a/x + "/" + b/x)
}
</script>
julianff
  • 140
  • 1
  • 1
  • 10
  • I understand what you mean but I have no idea how to write it in JavaScript. Is it possible if you write a short example? – Leo L. Jun 25 '17 at 18:04