# Reduce a fraction to its simplest form by using JavaScript

• Last Updated : 21 Feb, 2022

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.

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:

## javaScript

 `                   `
• 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:

## html

 `<``script` `type = "text/javaScript">` `// Simplified fraction by finding the GCD and dividing by it.``function reduce(number,denomin){``  ``var gcd = function gcd(a,b){``    ``return b ? gcd(b, a%b) : a;``  ``};``  ``gcd = gcd(number,denomin);``  ``return [number/gcd, denomin/gcd];``}` `document.write(reduce(15,20) + "<``br``>");` `document.write(reduce(84810,985612));``                   `
• Output:

```3,4
42405,492806```

My Personal Notes arrow_drop_up