- Related Questions & Answers
- Finding three desired consecutive numbers in JavaScript
- Three strictly increasing numbers (consecutive or non-consecutive). in an array in JavaScript
- JavaScript to check consecutive numbers in array?
- Check if three consecutive elements in an array is identical in JavaScript
- N consecutive odd numbers JavaScript
- How to check if array contains three consecutive dates in java?
- Sum of consecutive numbers in JavaScript
- Check if list contains consecutive numbers in Python
- Program to print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime Using C++
- Check whether given three numbers are adjacent primes in Python
- Program to find largest of three numbers - JavaScript
- Check if a number can be written as sum of three consecutive integers in C++
- Find the greatest product of three numbers in JavaScript
- Consecutive Numbers Sum in C++
- Maximum subsequence sum such that no three are consecutive

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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. Following is the code −

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));

Following is the output in the console −

[ 17, 18, 19 ]

Advertisements