[英]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.