 
  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
Finding three desired consecutive numbers in JavaScript
We are required to write a JavaScript function that takes in a Number, say n, and we are required to check whether there exist such three consecutive natural numbers (not decimal/floating point) whose sum equals to n.
If there exist such numbers, our function should return them, otherwise it should return false.
Example
The code for this will be −
const sum = 54; const threeConsecutiveSum = sum => {    if(sum < 6 || sum % 3 !== 0){       return false;    }    // three numbers will be of the form:    // x + x + 1 + x + 2 = 3 * x + 3    const residue = sum - 3;    const num = residue / 3;    return [num, num+1, num+2]; }; console.log(threeConsecutiveSum(sum));  Output
Following is the output on console −
[ 17, 18, 19 ]
Advertisements
 