- 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
Check three consecutive numbers - 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. Following is the code −
Example
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 in the console −
[ 17, 18, 19 ]
- Related Articles
- Finding three desired consecutive numbers in JavaScript
- JavaScript to check consecutive numbers in array?
- Three strictly increasing numbers (consecutive or non-consecutive). in an array in JavaScript
- Check if three consecutive elements in an array is identical in JavaScript
- N consecutive odd numbers JavaScript
- Sum of consecutive numbers in JavaScript
- Check if list contains consecutive numbers in Python
- How to check if array contains three consecutive dates in java?
- Program to print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime Using C++
- Program to check three consecutive odds are present or not in Python
- The sum of the squares of three consecutive natural numbers is 149. Find the numbers.
- The sum of three consecutive even number is 96 . Find the numbers
- The sum of three consecutive odd numbers is 39. Find the number
- The sum of three consecutive multiples of 2 is 18. Find the numbers.
- The sum of the three consecutive even number is 96. Find the numbers.

Advertisements