简体   繁体   English

为什么 Number() function 在我这样使用时不起作用?

[英]Why is the Number() function not working when I use it this way?

 function sumDigits(num) { // your code here let nums = num.toString(); let sum = 0; for(let i = 0;i<nums.length;i++){ Number(nums[i]); console.log(typeof nums[i]); sum += nums[i]; } return sum; } var output = sumDigits(1148); console.log(output); // --> 14

Can someone explain why Number(nums[i]) does not change the type of the variable.有人可以解释为什么 Number(nums[i]) 不改变变量的类型。 Or any other way to solve this problem?或任何其他方式来解决这个问题? The function is supposed to take in an integer value and add all the digits and return an integer. function 应该接受 integer 值并将所有数字相加并返回 integer。

When I tried to run the code i couldn't get the string to convert back into a number and would just add the value of sums to the front of the string.当我尝试运行代码时,我无法将字符串转换回数字,只能将总和的值添加到字符串的前面。

You are not saving the value returned by Number(nums[i]) .您没有保存Number(nums[i])返回的值。 But since here you are not using it more than once, you can just add it to the sum without assigning it to a variable.但是因为在这里你没有多次使用它,所以你可以将它添加到总和中而不用将它分配给变量。

 function sumDigits(num) { // your code here let nums = num.toString(); let sum = 0; for (let i = 0; i < nums.length; i++) { sum += Number(nums[i]); } return sum; } var output = sumDigits(1148); console.log(output); // --> 14

Number does not change the value it's used on. Number不会改变它所使用的值。 (It couldn't when used on a string, anyway, since strings in JavaScript are immutable.) It just returns a number. (无论如何,它不能用于字符串,因为 JavaScript 中的字符串是不可变的。)它只返回一个数字。 So you have to use that return value.所以你必须使用那个返回值。

Try this instead:试试这个:

for(let i = 0; i < nums.length; i++){
    sum += Number(nums[i]);
}

To fix your code, change sum += nums[i];要修复您的代码,请更改sum += nums[i]; to sum += Number(nums[i]); sum += Number(nums[i]); . . Reason: When you add a string to a number, the number is first converted to a string, and a string concatenation is performed.原因:给数字添加字符串时,先将数字转换为字符串,然后进行字符串拼接。

Here is a functional programming approach to your question:这是针对您的问题的函数式编程方法:

 function sumDigits(num) { return num.toString() // convert number to string.split('') // split into array of chars.reduce((acc, val) => { // reduce array to sum of items return acc + Number(val) }, 0); } var output = sumDigits(1148); console.log(output); // --> 14

Functional programming makes code more extensible, modular, reusable, performing, and testable.函数式编程使代码更具可扩展性、模块化、可重用性、执行性和可测试性。

Into to functional programming in JavaScript: https://opensource.com/article/17/6/functional-javascript进入函数式编程JavaScript: https://opensource.com/article/17/6/functional-javascript

.reduce() docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce .reduce .reduce()文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

Here is the same with a map reduce approach:这与 map reduce 方法相同:

 function sumDigits(num) { return num.toString() // convert number to string.split('') // split into array of chars.map(char => Number(char)) // change each char in array to number.reduce((acc, val) => { // reduce array to sum of items return acc + val }, 0); } var output = sumDigits(1148); console.log(output); // --> 14

Intro to map reduce: https://medium.com/poka-techblog/simplify-your-javascript-use-map-reduce-and-filter-bd02c593cc2d map reduce 简介: https://medium.com/poka-techblog/simplify-your-javascript-use-map-reduce-and-filter-bd02c593cc2d

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

相关问题 为什么当我使用 php 时 onchange 不起作用 - why onchange is not working when i use php with it 当我在函数中使用参数时,setinterval不起作用 - setinterval is not working when I use parameter in function 为什么当我尝试使用 props 传递的函数时 preventDefault 停止工作? - Why does preventDefault stop working when I try to use a function passed by props? 当我使用 document.write 函数打印下面代码中的数字时,它会在每个输出的右侧插入 0。为什么会这样? - When I use the document.write function to print the number in code below then it insert the 0 on right side of each output.Why it is happening? 为什么我触发功能时输入类型=数字值为空 - why is input type = number value is empty when i trigger function 为什么在使用 jQuery 时滚动到顶部不起作用? - Why scroll to top not working when I use jQuery? 为什么当我使用 addEventListener 时复选框停止工作 - why does checkbox stops working when ever i use addEventListener 为什么在使用Bootstrap 4时粘性标头在表单标签中不起作用? - Why sticky header is not working in form tags when I use bootstrap 4? 当我尝试使用IFFE时,为什么这段代码不起作用? - Why isn't this code working when i try to use IFFE 当我在div中加载脚本时,为什么此功能停止工作? - Why this function stops working when I load the script inside a div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM