getSum(); function getSum() { var count = 0; //计算总件数 var money = 0; //计算总价钱 $(".itxt").each(function(i,ele) { count += parseInt($(ele).val()); }) $(".amount-sum em").text(count); $(".p-sum").each(function(i,ele) { money += parseFloat($(ele).text().substr(1)); }) $(".price-sum em").text("¥" + money.toFixed(2)); }
时间: 2024-03-31 21:37:45 浏览: 21
这是一段JavaScript代码,函数名为getSum(),它会获取页面上所有带有class为itxt的元素的值并计算它们的总和,然后将结果显示在class为amount-sum的元素中。接着它会获取页面上所有带有class为p-sum的元素的值并计算它们的总和,然后将结果显示在class为price-sum的元素中,保留两位小数。这段代码的作用是计算购物车中商品的总件数和总价钱。
相关问题
用函数求出两个数据的+ -*/ function getSum
好的,以下是一个求两个数据的加减乘除的函数示例:
```python
def getSum(num1, num2, operator):
if operator == '+':
return num1 + num2
elif operator == '-':
return num1 - num2
elif operator == '*':
return num1 * num2
elif operator == '/':
if num2 == 0:
return "除数不能为0"
else:
return num1 / num2
else:
return "不支持的运算符"
```
你可以传入两个数字和一个操作符来调用该函数,例如:
```python
print(getSum(2, 3, '+')) # 输出 5
print(getSum(5, 3, '-')) # 输出 2
print(getSum(2, 3, '*')) # 输出 6
print(getSum(6, 3, '/')) # 输出 2.0
print(getSum(8, 0, '/')) # 输出 "除数不能为0"
print(getSum(2, 3, '%')) # 输出 "不支持的运算符"
```
java计算1+1/2!+1/3!+…+1/20!
这个Java程序的目的是计算1+1/2!+1/3!+…+1/20!的值。其中,1/n!表示n的阶乘的倒数。程序中使用了循环和方法来实现计算。具体实现过程为:先输入一个数a,然后调用sum(a)方法,该方法使用两个循环来计算每个数的阶乘和总和,最后输出总和。其中,sum1表示每个数的阶乘,sum2表示总和。程序中还包括了一个父类calculate和两个派生类calculate_mul_x和calculate_div_x,它们分别用于计算不同的表达式。calculate类中的getSum方法用于计算1+1/2!+1/3!+…+1/n!,calculate_mul_x类中的getSum方法用于计算x+x/2!+x/3!+…+x/n!calculate_div_x类中的getSum方法用于计算1+1/(2!*x)+1/(3!*x)+…+1/(n!*x)。在主类mainClass中,分别创建了calculate、calculate_mul_x和calculate_div_x的对象,并调用它们的getSum方法进行计算。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)