Converting a proper fraction to mixed fraction - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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 ]
raja
Published on 18-Sep-2020 09:19:27
Advertisements