- Related Questions & Answers
- Check three consecutive numbers - JavaScript
- Finding desired numbers in a sorted array in JavaScript
- Three strictly increasing numbers (consecutive or non-consecutive). in an array in JavaScript
- Finding longest consecutive joins in JavaScript
- Finding desired sum of elements in an array in JavaScript
- N consecutive odd numbers JavaScript
- Finding if three points are collinear - JavaScript
- Sum of consecutive numbers in JavaScript
- Finding tidy numbers - JavaScript
- Finding clusters of consecutive negative integers in JavaScript
- Finding perfect numbers in JavaScript
- JavaScript to check consecutive numbers in array?
- Program to print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime Using C++
- Finding pandigital numbers using JavaScript
- Finding two golden numbers in JavaScript

- 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.

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

Following is the output on console −

[ 17, 18, 19 ]

Advertisements