+ 1
Hey Guys! How do you add,subtract,multiply and divide Fractions in Javascript based on the user's Input?
Thanks Guys!
4 ответов
+ 3
By implementing functions (or better classes) for handling fractions computation... or prebuilded library ^^
Implementing such library would require handling prime numbers and GCD, to be able to reduce fractions, and operation are done with basic mathematicals rules:
a/b + c/d = (a*d + c*b) / (b*d)
a/b * c/d = (a*b) / (c*d)
... and obviously:
a-b = a+(-b)
a/b = a*(1/b)
... so:
a/b - c/d = (a*d - c*b) / (b*d)
(a/b) / (c/d) = a/b * d/c
+ 2
Fractions? I would suggest converting the fractions into float values (3/4 --> 0.75), performing the math on the float, and converting it back to a decimal. Decimal to Fraction conversion is not difficult to program a function for.
+ 2
Bonus code/tutorial for GCD and prime factor:
https://code.sololearn.com/WPXD8LSoeiIL/?ref=app
https://code.sololearn.com/WteKxMC4vDvC/?ref=app
First part is dedicated to implementation of prime number optimized function, second one implement GCD function (using prime number function)...
0
ohh I See, Thanks Guys!