Home  >  Article  >  Web Front-end  >  How to find the sum of powers in javascript

How to find the sum of powers in javascript

青灯夜游
青灯夜游Original
2022-10-10 16:37:261627browse

Steps to find the sum of powers: 1. Use the pow() function of the Math object to find the Nth power of the specified number respectively. The syntax "Math.pow(specified number, N)" will return the specified number. The power value of the number; 2. Use the " " operator to add up the multiple power values ​​obtained. The syntax is "power value 1 power value 2 power value 3....".

How to find the sum of powers in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In javascript, you can use the pow() function of the Math object and the " " operator to find the sum of powers.

Implementation steps:

#1. Use the pow() function to find the Nth power of several specified numbers

Example: Find the 2nd power of 1, 2, 3, 4, 5

var n1=Math.pow(1, 2);
var n2=Math.pow(2, 2);
var n3=Math.pow(3, 2);
var n4=Math.pow(4, 2);
var n5=Math.pow(5, 2);
console.log(n1);
console.log(n2);
console.log(n3);
console.log(n4);
console.log(n5);

How to find the sum of powers in javascript

2, use the " " operator Add up the obtained power values

var sum=n1 + n2 + n3 + n4 + n5;

How to find the sum of powers in javascript

##Expand knowledge:

The pow() method of the Math object

Math.pow() is an exponentiation function, and the syntax is

Math.pow(x,y)

  • x Required. base. Must be a number.​

  • #y Required. Power number. Must be a number.

can return a value representing x raised to the power of y.

Example:


console.log(Math.pow(0, 1));
console.log(Math.pow(1, 0));
console.log(Math.pow(1, 1));
console.log(Math.pow(1, 10));
console.log(Math.pow(3, 3));
console.log(Math.pow(-3, 3));
console.log(Math.pow(2, 4));

How to find the sum of powers in javascript

Note: If the result is an imaginary or negative number, this method will return NaN. If a floating point overflow occurs due to an exponent that is too large, this method returns Infinity.

Addition operator " "

##Operator Example 1
Description Example
Addition operator x y means calculating the sum of x plus y

Pay attention to the summation operation of special operands.

var n = 5;  //定义并初始化任意一个数值
console.log(NaN + n);  //NaN与任意操作数相加,结果都是NaN
console.log(Infinity + n);  //Infinity与任意操作数相加,结果都是Infinity
console.log(Infinity + Infinity);  //Infinity与Infinity相加,结果是Infinity
console.log((-Infinity) + (-Infinity));  //负Infinity相加,结果是负Infinity
console.log((-Infinity) + Infinity);  //正负Infinity相加,结果是NaN

Example 2

The addition operator can decide whether to add or connect based on the data type of the operands.

console.log(1 + 1);  //如果操作数都是数值,则进行相加运算
console.log(1 + "1");  //如果操作数中有一个是字符串,则进行相连运算
console.log(3.0 + 4.3 + "");  //先求和,再连接,返回"7.3"
console.log(3.0 + "" + 4.3);  //先连接,再连接,返回"34.3"
                              //3.0转换为字符串3

When using the addition operator, you should first check whether the data type of the operand meets the requirements.

[Recommended learning:

javascript advanced tutorial

]

The above is the detailed content of How to find the sum of powers in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn