How to get the sum of the powers of all numbers in JavaScript?


In this tutorial, we'll be discussing how to get the sum of the powers of all the numbers from start to end in JavaScript. We'll be using the built-in Math.pow() method to calculate the powers and the Array reduce() method to sum up the values.

Using the Math.pow() method

The Math.pow() method allows us to calculate the power of a number. We can use this method to calculate the powers of all the numbers from start to end.

Syntax

Math.pow(base, exponent) ;

Parameters

  • Base − The base number.

  • Exponents − The exponent to which to raise the base.

Example

For example, if we want to get the sum of the power 2 of all the numbers from 1 to 4, we can do so by using the following code −

<html>
<head>
   <title>Examples</title>
</head>
<body>
   <div id="result"></div>
   <script>
      var sum = 0;
      for(var i=1; i<=4; i++) {
          sum += Math.pow(i, 2);
      }
      document.getElementById("result").innerHTML = sum;
   </script>
</body>
</html>

In the code above, we first created a variable named sum and initialized it to 0. We then used a for loop to iterate through all the numbers from 1 to 4. For each number, we calculate its power and add it to the sum. Finally, we print out the sum.

Using the Array.reduce() method

Another way to get the sum of the powers of all the numbers from start to end is to use the Array.reduce() method.

The Array.reduce() method allows us to reduce an array to a single value. We can use this method to sum up the values of an array.

Example

For example, if we want to get the sum of the power 3 of all the numbers from 1 to 4, we can do so by using the following code −

<html>
<head>
   <title>Example: Using the Array.reduce() method </title>
</head>
<body>
   <div id="result"></div>
   <script>
      var numbers = [1, 2, 3, 4];
      var sum = numbers.reduce(function(a, b) {
         return a + Math.pow(b, 3);
      }, 0);
      document.getElementById("result").innerHTML = sum;
   </script>
</body>
</html>

In the code above, we first created an array named numbers which contains all the numbers from 1 to 4. We then used the Array.reduce() method to sum up the values. The function we passed to the Array.reduce() method calculates the power 3 of each number and adds it to the sum. Finally, we print out the sum.

Using the ES6 arrow function −

We can also use the ES6 arrow function to get the sum of the powers of all the numbers from start to end.

Example

For example, if we want to get the sum of the powers of all the numbers from 1 to 4, we can do so by using the following code −

<html>
<head>
   <title>Example: Using the ES6 arrow function</title>
</head>
<body>
   <div id="result"></div>
   <script>
      var numbers = [1, 2, 3, 4];
      var sum = numbers.reduce((a, b) => a + Math.pow(b, 2), 0);
      document.getElementById("result").innerHTML = sum      
   </script>
</body>
</html>

In the code above, we first created an array named numbers which contains all the numbers from 1 to 4. We then used the Array.reduce() method to sum up the values. The arrow function we passed to the Array.reduce() method calculates the power of each number and adds it to the sum. Finally, we print out the sum.

Conclusion

In this article, we've discussed how to get the sum of the powers of all the numbers from start to end in JavaScript. We've used the built-in Math.pow() method to calculate the powers and the Array.reduce() method to sum up the values.

Updated on: 02-Jul-2022

353 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements