简体   繁体   English

如何制作减去数字的参数函数

[英]How to make an argument function which subtracts numbers

I want the first number to be positive, like 10-20-30, not -10-20-30我希望第一个数字是正数,比如 10-20-30,而不是 -10-20-30

 function substractAll() { var sum = 0 for (var i = 0; i < arguments.length; i++) { sum -= arguments[i] } console.log(sum) } substractAll(10, 20, 30)

You can extract the first argument first in the parameter list:您可以首先提取参数列表中的第一个参数:

 function substractAll(initial, ...rest) { var sum = initial; for (var i = 0; i < rest.length; i++) { sum -= rest[i]; } console.log(sum) } substractAll(10, 20, 30)

Or use .reduce , and depend on the initial value for the .reduce callback being the first accumulator, the only positive value, thanks @JaromandaX:或者使用.reduce ,并依赖于.reduce回调的初始值是第一个累加器,唯一的正值,谢谢@JaromandaX:

 function substractAll(...args) { const sum = args.reduce((a, b) => a - b); console.log(sum) } substractAll(10, 20, 30)

Start by setting sum to the first number instead of 0 , then loop over the remaining numbers.首先将sum设置为第一个数字而不是0 ,然后遍历剩余的数字。

 function substractAll() { var sum = arguments[0]; for (var i = 1; i < arguments.length; i++) { sum -= arguments[i] } console.log(sum) } substractAll(10, 20, 30)

You can assign the first argument to sum as positive and then iterate the other arguments starting the for loop at index = 1:您可以将 sum 的第一个参数指定为正数,然后迭代其他参数,从 index = 1 处开始 for 循环:

 function substractAll(){ var sum = 0; sum = arguments[0]; for(let i = 1; i < arguments.length; i++){ sum -= arguments[i] } console.log(sum) } substractAll (10, 20, 30)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在谷歌应用程序脚本中,我将如何制作一个 function 从另一列中的所有相应单元格中减去一列中的所有单元格 - In google app scripts, how would I make a function that subtracts all the cells in one column from all the respective cells in another column 您如何将传递给函数的任意数量的参数应用于JavaScript中另一个函数的参数? - How do you apply an arbitrary numbers of arguments passed to function which is argument of another function in JavaScript? 我们如何获得两次递归调用的最小值,一个是加法,另一个是减法? - How could we get min of two recursive calls, one which adds, another which subtracts? 我如何为它制作一个 function 来跟踪第二个参数的空值 - How would i make a function for it as well which will keep track of the empty values of second argument 如何在JavaScript中的函数(本身就是一个参数)中传递参数 - How argument is being passed in the function(which itself is an argument) in JavaScript 如何制作一个读取日期(小时,分钟,秒)的程序,然后从第二个日期中减去它 - How can I make a program that reads the date(hour,minutes,seconds) then it subtracts it from the second date 如何使像参数一样的elementid的javascript函数? - How to make a javascript function with elementid like argument? 即时通讯使用onkeyup()来调用javascript函数,该函数会添加一些值,而在使用退格键时,它不会减去该值,而是会添加每个值 - im using onkeyup() to call javascript function which adds some value, while using backspace it not subtracts the value instead it adds every values 如何创建一个函数来计算带小数的数字的阶乘? - How to make a function that computes the factorial for numbers with decimals? 测试作为函数的函数参数 - Testing a function argument which is a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM