Hey Guys! How do you add,subtract,multiply and divide Fractions in Javascript based on the user's Input? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Hey Guys! How do you add,subtract,multiply and divide Fractions in Javascript based on the user's Input?

Thanks Guys!

30th Jun 2017, 10:05 AM
Teddy Pascual
Teddy Pascual - avatar
4 Answers
+ 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
30th Jun 2017, 5:04 PM
visph
visph - avatar
+ 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.
30th Jun 2017, 12:13 PM
T Sacrin
T Sacrin - avatar
+ 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)...
30th Jun 2017, 6:26 PM
visph
visph - avatar
0
ohh I See, Thanks Guys!
30th Jun 2017, 6:20 PM
Teddy Pascual
Teddy Pascual - avatar