Finding two golden numbers in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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 than our function should return false.

Example

The code for this will be −

const goldenNumbers = (sum, prod) => {
   for(let i = 0; i < (sum / 2); i++){
      if(i * (sum-i) !== prod){
         continue;
      };
      return [i, (sum-i)];
   };
   return false;
};
console.log(goldenNumbers(24, 144));
console.log(goldenNumbers(14, 45));
console.log(goldenNumbers(21, 98));

Output

The output in the console −

false
[ 5, 9 ]
[ 7, 14 ]
raja
Published on 14-Oct-2020 07:54:20
Advertisements