Work with fractions in Javascript

0

Good! I am creating a super simple calculator through forms, it is only based on this proposed systems of equations resolver: link

The problem that arises is that when entering a fraction of the type "x / y", the parseFloat that is in the line that contains this cycle for inside the gauss () function reduces it to only x, eliminating the denominator and invalidating the division:

     for(i=1;i<=3;i++){ 

          a[i]=[0,0,0,0],b[i]=[0,0,0,0],c[i]=[0,0,0,0]
      for(j=1;j<=4;j++){ 
          a[i][j]=parseFloat(document.forms[0][4*i+j-5].value) 
      }
    }

Any ideas on how to make the fractions of the "x / y" type resolve before the parseFloat cuts them, so that the decimal is saved at one time? I understand that it should not be so difficult, since in the console I tried to insert a fraction "1/2" inside an array, and this was saved as a decimal at once, but I'm just learning the very basic and I do not move for sure for programming.

Attentive to your answers, and thanks in advance!

    
asked by mrojas6996 31.10.2018 в 20:45
source

1 answer

0

parseFloat only accepts strings that represent a number in decimals (eg '3.14' , '314e-2' , '0.0314E+2' ), does not recognize fractions.

What you can do is find the operator / in the string

var str = '3/4';
var operandos = str.split('/'); // esto te devuelve un array ['3', '4']
var resultado = parseInt(operandos[0]) / parseInt(operandos[1]);
    
answered by 31.10.2018 / 20:59
source