A fraction is simplified if there are no common factors (except 1) between the numerator and the denominator. For example, 4/6 is not simplified, since 4 and 6 both share 2 as a factor. If improper fractions can be transformed into integers. There are two possible ways to simplify the fraction by using JavaScript.
- Using math.js simplify() Function
- Using JavaScript _.reduce() Function
Below examples will illustrate the approach:
Using math.js simplify() function: In this function few rules are applied on the expression, you can create your own custom made rules. Basically this function is like lambda function. Where you can make the rules according to your requirements.
- Example:
Input :simplify("4/6") Output :"2/3" - Program:
<script type ="text/javaScript">// main functionfunctionsimplify(str) {varresult ='', data = str.split('/'),numOne = Number(data[0]),numTwo = Number(data[1]);for(vari = Math.max(numOne, numTwo); i > 1; i--) {if((numOne % i == 0) && (numTwo % i == 0)) {numOne /= i;numTwo /= i;}}if(numTwo === 1) {result = numOne.toString()}else{result = numOne.toString() +'/'+ numTwo.toString()}returnresult}document.write(simplify("4/6") +"<br>");document.write(simplify(84810,985612));</script>chevron_rightfilter_none - Output:
2/3 42405/492806
Using JavaScript _.reduce() function: The _.reduce() is an inbuilt function in JavaScript which is used to transform an array’s / object’s properties into one single value or is used to create a single result from a given list of values. When all the elements of the list are passed to the function/iterate and no more elements remain then the _.each loop ends. Here we will find the GCD of those numbers and dividing it by GCD we can make the output simpler.
- Example:
Input: 15, 20 Output: 3,4
- Program:
<scripttype="text/javaScript">// Simplified fraction by finding the GCD and dividing by it.function reduce(numer,denomin){var gcd = function gcd(a,b){return b ? gcd(b, a%b) : a;};gcd = gcd(numer,denomin);return [numer/gcd, denomin/gcd];}document.write(reduce(15,20) + "<br>");document.write(reduce(84810,985612));</script>chevron_rightfilter_none - Output:
3,4 42405,492806
Recommended Posts:
- Design a form component which takes input from its user and displays a form
- How to map, reduce and filter a Set element using JavaScript ?
- JavaScript | typedArray.reduce() with Examples
- JavaScript Array reduce() Method
- JavaScript TypeError - Reduce of empty array with no initial value
- Ways To Reduce The Loading Time Of Website | Set 1
- Ways To Reduce The Loading Time Of Your Website | Set 2
- Underscore.js | _.reduce() Method
- PHP | Ds\Vector reduce() Function
- PHP | Ds\Deque reduce() Function
- PHP | Ds\Set reduce() Function
- PHP | Ds\Map reduce() Function
- PHP | Ds\Sequence reduce() Function
- Reduce the size of an icon during the animation
- TypeScript | Array reduce() Method
- Lodash _.reduce() Method
- Set the focus to HTML form element using JavaScript
- How to get form data using JavaScript/jQuery?
- How to clear form after submit in Javascript without using reset?
- Form validation using HTML and JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


