- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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 sum of all numbers within a range in JavaScript
Problem
We are required to write a JavaScript function that takes in an array that specifies a range.
Our function should find and return the sum of all the natural numbers falling in the range including the range numbers.
Example
Following is the code −
const range = [4, 67]; const findSum = ([l, h]) => { let sum = 0; for(let i = l; i <= h; i++){ sum += i; }; return sum; }; console.log(findSum(range));
Output
Following is the console output −
2272
Advertisements