JavaScript: Get the sum of the powers of all the numbers from start to end
JavaScript fundamental (ES6 Syntax): Exercise-135 with Solution
Sum of Powers in Range
Write a JavaScript program to get the sum of the powers of all the numbers from start to end (both inclusive).
- Use Array.prototype.fill() to create an array of all the numbers in the target range.
- Use Array.prototype.map() and the exponent operator (**) to raise them to power and Array.prototype.reduce() to add them together.
- Omit the second argument, power, to use a default power of 2.
- Omit the third argument, start, to use a default starting value of 1.
Sample Solution:
JavaScript Code:
Output:
385 3025 2925
Visual Presentation:
Flowchart:

Live Demo:
For more Practice: Solve these Related Problems:
- Write a JavaScript program that calculates the sum of the k-th powers of all integers between two given numbers.
- Write a JavaScript function that iterates from start to end and sums up each number raised to a specified power.
- Write a JavaScript program that uses a loop or reduce() to compute the sum of powers for a range of integers.
Improve this sample solution and post your code through Disqus
Previous: Write a JavaScript program to get the symmetric difference between two given arrays.
Next: Write a JavaScript program to generate all permutations of a string (contains duplicates).
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.