 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Take two numbers m and n & return two numbers whose sum is n and product m in JavaScript
We are required to write a JavaScript function that takes in two numbers, say m and n and returns two numbers whose sum is n and product is m. If there exist no such numbers, then our function should return false
Let’s write the code for this function −
Example
const perfectNumbers = (sum, prod) => {    for(let i = 0; i < (sum / 2); i++){       if(i * (sum-i) !== prod){          continue;       };       return [i, (sum-i)];    };    return false; }; // 12 12 are not two distinct numbers console.log(perfectNumbers(24, 144)); console.log(perfectNumbers(14, 45)); console.log(perfectNumbers(21, 98));  Output
Following is the output in the console −
false [ 5, 9 ] [ 7, 14 ]
Advertisements
 