我已经构建了一个数字计算器的开端,并希望有经验的javascript开发人员来检查这段代码。
它按预期工作,但我不得不认为有更好的方法来编写它,而不是使用多个for循环。
需要做一些计算,所以我看不出有什么办法可以绕过它。任何方向都会很好。
function addNumbers(arr, fn) {
var arrayTotal = [],
singleDigits = [];
for (i = 0; i < arr.length; i++) {
arrayTotal.push(
fn(arr[i])
);
}
for (value in arrayTotal) {
for (num in arrayTotal[value]) {
singleDigits.push(parseInt(arrayTotal[value][num]));
}
}
for (i = 0, sum = 0; i < singleDigits.length; i++) {
sum += singleDigits[i];
}
for (i = 0, numericValue = 0, sum = fn(sum); i < sum.length; i++) {
numericValue += parseInt(sum[i]);
}
return numericValue;
}
function splitNumbers(item) {
return item.toString().split('');
}
var value = addNumbers([13, 11, 1938], splitNumbers);
document.body.innerHTML = value;发布于 2015-09-23 03:35:18
map和reduce是为这样的问题量身定做的。
要理解本文末尾代码的最终版本中的while循环,可能需要考虑一个"longhand“版本,它可以做同样的事情:
// this sets the answer we seek to the sum of all the digits
// of every number passed in via the array
finalAnswer = addDigits(combinedDigits);
// but since this sum may still be larger than a 1 digit
// number (ie, 10 or more), we need to continue the summing
// the digits until we end up with a single digit number
while ( finalAnswer > 9 ) {
// this converts an integer to a string
combinedDigits = '' + finalAnswer;
// this sums the digits again
finalAnswer = addDigits(combinedDigits);
}这是最后的,完整的版本,与更紧凑的时间循环。我们只是简单地结合了上面的一些步骤。
function addDigitsRecursively(arr) {
var combinedDigits = arr.join(''), finalAnswer;
while ( (finalAnswer = addDigits(combinedDigits)) > 9 )
combinedDigits = '' + finalAnswer;
function addDigits(str) {
return str.split('').map(function(n) {return parseInt(n)})
.reduce(function(a, b){return a+b;})
}
return finalAnswer;
}
var value = addDigitsRecursively([13, 11, 1938]);
document.body.innerHTML = value;发布于 2019-12-01 21:29:35
这个答案是基于约拿的,这消除了重新定义,它减少了代码的大小,取而代之的是用新的结果覆盖先前的结果,并使用它作为输入。它拆分数字,添加组件,然后将输入设置为输出并重复。
+b运算符是将字符串转换为int的缩写。
function addDigits(inputArray) {
str = inputArray.join('')
while ((str = str.split('').reduce((a, b) => +a + +b)+'') > 9);
return +str
}
console.log(addDigits([13, 11, 1938]))
>>> 9它可以进一步简化,但正在进入难以理解的领域:
function addDigits(c) {
while ((c = (c + '').split('').reduce((a, b) => (+a || 0) + (+b || 0))) > 9);
return c
}如果不允许.split(''),那么.
function addDigits(c) {
c = c.join('')
while ((c = Array.from(Array(Math.ceil(Math.log10(c)))
.keys())
.map(p => Math.floor(c % Math.pow(10, p + 1) / Math.pow(10, p)))
.reduce((a, b) => a + b)) > 9);
return c
}发布于 2015-09-23 07:59:25
我只是想把这个发出去。在阅读了map()和reduce()之后,我意识到它们只得到IE9+的支持。从Jonah的文章中得到了一些灵感,我使用了两个for循环来精简代码。见下文。
function mapForEach(arr) {
var combinedNumbers = arr.join(''),
totalValue = 0,
finalValue = 0;
for (i = 0; i < combinedNumbers.length; i++) {
totalValue += parseInt(combinedNumbers[i]);
}
for (i = 0, totalValue += ''; i < totalValue.length; i++) {
finalValue += parseInt(totalValue[i]);
}
return finalValue;
}
var myArray = [13, 11, 1938],
value = mapForEach(myArray);
document.body.innerHTML = value;https://codereview.stackexchange.com/questions/105427
复制相似问题