- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 ]
Advertisements