Converting a proper fraction to mixed fraction - JavaScript


Proper Fraction

A proper fraction is the one that exists in the p/q form (both p and q being natural numbers)

Mixed Fraction

Suppose we divide the numerator of a fraction (say a) with its denominator (say b), to get quotient q and remainder r.

The mixed fraction form for fraction (a/b) will be −

qrb

And it is pronounced as "q wholes and r by b”.

We are required to write a JavaScript function that takes in an array of exactly two numbers representing a proper fraction and our function should return an array with three numbers representing its mixed form

Example

Following is the code −

const arr = [43, 13];
const properToMixed = arr => {
   const quotient = Math.floor(arr[0] / arr[1]);
   const remainder = arr[0] % arr[1];
   if(remainder === 0){
      return [quotient];
   }else{
      return [quotient, remainder, arr[1]];
   };
};
console.log(properToMixed(arr));

Output

Following is the output in the console −

[ 3, 4, 13 ]

Updated on: 18-Sep-2020

317 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements