- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Finding two golden numbers 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 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 ]
- Related Articles
- Finding perfect numbers in JavaScript
- Finding two prime numbers with a specific number gap in JavaScript
- Finding tidy numbers - JavaScript
- Finding pandigital numbers using JavaScript
- Finding two numbers that produce equal to the sum of rest in JavaScript
- Finding two missing numbers that appears only once and twice respectively in JavaScript
- Finding three desired consecutive numbers in JavaScript
- Finding two numbers given their sum and Highest Common Factor using JavaScript
- Finding matches in two elements JavaScript
- Finding special type of numbers - JavaScript
- Finding lunar sum of Numbers - JavaScript
- Finding Lucky Numbers in a Matrix in JavaScript
- Finding union of two sets in JavaScript
- Finding gcd of two strings in JavaScript
- Finding Armstrong numbers in a given range in JavaScript

Advertisements