- 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
Summing cubes of natural numbers within a range in JavaScript
Problem
We are required to write a JavaScript function that takes in a range array of two numbers. Our function should find the sum of all the cubes of the numbers that falls in the specified range.
Example
Following is the code −
const range = [4, 11]; const sumCubes = ([l, h]) => { const findCube = num => num * num * num; let sum = 0; for(let i = l; i <= h; i++){ sum += findCube(i); }; return sum; }; console.log(sumCubes(range));
Output
4320
Advertisements