简体   繁体   English

有人可以解释为什么我得到 9 和 5 而不是 36880 和 375

[英]Can someone explain why i'm getting 9 & 5 instead of 36880 & 375

For my return, I'm getting 9 and 5 instead of 362880 and 375. Can someone explain what I missed?对于我的回报,我得到 9 和 5 而不是 362880 和 375。有人可以解释我错过了什么吗?

 function multiplyAll(num){ // declare a function name multyplyAll with a num parameter for (let i = 0; i < num.length; i++){ // for loop to iterate base on the length of the parameter. num *= num[i] // multiplies them all together. } return num // function outpout } console.log(multiplyAll(9, 4, 5, 6, 7, 2, 1, 8, 3)) // should log: 362880 console.log(multiplyAll(5, 5, 5, 3)) // should log: 375

When you are using num without rest parameters then only the first value will get assign to num .当您使用没有rest parametersnum时,只有第一个值将分配给num You are treating like num is an array but num is just of type number您将num一个数组,但num只是 number 类型

There is no length property on num , Because num is of type number. num上没有length属性,因为num是 number 类型。 It won't run the for loop and return the first element ie 9 and 5它不会运行for循环并返回第一个元素,即95

You should use collect then in to an array using rest parameters您应该使用 collect 然后使用rest parameters进入数组

 function multiplyAll(...num) { // declare a function name multyplyAll with a num parameter let res = 1; for (let i = 0; i < num.length; i++) { // for loop to iterate base on the length of the parameter. res *= num[i]; // multiplies them all together. } return res; // function outpout } console.log(multiplyAll(9, 4, 5, 6, 7, 2, 1, 8, 3)); // should log: 362880 console.log(multiplyAll(5, 5, 5, 3)); // should log: 375

You can also use reduce here as:您也可以在此处使用reduce作为:

 function multiplyAll(...num) { return num.reduce((acc, curr) => acc * curr, 1); } console.log(multiplyAll(9, 4, 5, 6, 7, 2, 1, 8, 3)); // should log: 362880 console.log(multiplyAll(5, 5, 5, 3)); // should log: 375

暂无
暂无

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

相关问题 有人可以解释为什么我收到 500 内部错误吗? - Can someone explain why I'm getting 500 Internal Error? 有人可以向我解释为什么我在此上出现堆栈溢出吗? - Can someone explain to me why I am getting a Stack Overflow on this? 有人可以解释为什么此代码返回 6 6 6 6 6 而不是 1 2 3 4 5 - Can someone explain why this code returns 6 6 6 6 6 instead of 1 2 3 4 5 谁能解释为什么我收到 map function 错误 - can anyone explain why i'm getting a map function error 我正在尝试用javascript(下划线)重写备忘录,有人可以解释吗? - I'm trying to rewrite memoize in javascript (for underscore), can someone explain this? 我收到“icons[0].addClass("change") 不是函数”的错误。 有人可以解释一下,我该如何解决这个问题? - I'm getting an error that "icons[0].addClass("change") is not a function". Can someone please explain me that, how could I fix this? 有人可以解释为什么这不起作用? - Can someone explain why is this not working? 有人可以解释为什么我得到“ Object Required”错误 - Can someone explain why I get “Object Required” error 谁能解释为什么我收到有关“相同密钥”的错误 - Can anyone explain why I'm getting the error about "same key" 我是jquery的新手,正在学习snap.svg,不知道这段代码如何工作? 有人可以解释一下该函数如何获取值吗? - I'm new to jquery and learning snap.svg and don't kow how this code work? Can someone Please explain how the function is getting values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM