比较数组中元素的总和以确定哪个是最高的
Comparing sums of elements within an array to determine which is the highest
我正在尝试在 Javascript 中创建一个函数,它将获取数字数组的每个元素(在本例中具体为 phone 数字)并确定哪个元素的总和最高。我已经达到了我感到非常失败的地步,但我认为我已经非常接近了。任何人都可以提供一些指导吗?这是我目前所拥有的:
function highest(inputArray) {
var sum = 0;
var currentHighest = 0;
var largest = 0;
我设置了要使用的变量,然后创建了一个 for 循环来遍历数组中的每个元素。
for (a = 0; a < inputArray.length; a++)
var tempArray = inputArray[a].replace(/\D/g,'');
我创建了一个占位符字符串来删除元素中的所有非整数,然后创建一个函数来对元素的所有数字求和。
function sumDigits(str) {
for (i = 0; i < str.length; i++) {
sum += parseInt(str.charAt(i));
return sum;
}
}
然后创建一个 if 语句来测试当前元素的总和是否大于或等于最高总和元素。
if (sumDigits(tempArray) >= currentHighest) {
currentHighest = sum;
largest = inputArray[a];
return largest;
}
else {
return largest;
}
}
var newArray = ['123-456-7777', '963-481-7945', '111-222-3333'];
console.log(highest(newArray));
这里是整个代码块:
function highest(inputArray) {
var sum = 0;
var currentHighest = 0;
var largest = 0;
for (a = 0; a < inputArray.length; a++)
var tempArray = inputArray[a].replace(/\D/g,'');
function sumDigits(str) {
for (i = 0; i < str.length; i++) {
sum += parseInt(str.charAt(i));
return sum;
}
}
if (sumDigits(tempArray) >= currentHighest) {
currentHighest = sum;
largest = inputArray[a];
return largest;
}
else {
return largest;
}
}
}
var newArray = ['123-456-7777', '963-481-7945', '111-222-3333'];
console.log(highest(newArray));
当我 运行 代码时,如果有帮助,我会得到 "undefined" 作为结果。预先感谢您的帮助。
在您的代码中,您没有在此处初始化 sum
变量,而是在此函数中过早地返回了 sum
值:
function sumDigits(str) {
for (i = 0; i < str.length; i++) {
sum += parseInt(str.charAt(i));
return sum;
}
}
应该是这样的:
function sumDigits(str) {
var sum = 0;
for (i = 0; i < str.length; i++) {
sum += parseInt(str.charAt(i), 10);
}
return sum;
}
如果不在一个块中看到所有代码,我们就无法真正看出还有什么问题,因此我们可以看到不同部分如何相互调用和交互。
这是一个更紧凑的解决方案(假设您尝试对每个 phone 数字中的数字求和):
var phoneNumbers = ["123-456-7890", "982-111-9999"];
var sums = phoneNumbers.map(function(p) {
return p.match(/\d/g).reduce(function(sum, num) {
return sum + parseInt(num, 10);
}, 0);
});
var maxSum = Math.max.apply(Math, sums);
// output results in the snippet window
document.write("sums = " + JSON.stringify(sums) + "<br>");
document.write("maxSum = " + maxSum + "<br>");
工作原理如下:
- 运行
.map()
在 phone 数字数组上,目的是返回一个总和数组。
- 在
.map()
中搜索所有数字,然后在结果数组上 运行 .reduce()
累加总和。
- 然后,要获取
sums
数组中的最大值,请使用 Math.max()
可以接受整个数组并为您完成最大工作。
如果我对这个问题的解释是正确的(将 phone 数字的每个数字相加,然后打印出最大的结果),你可以这样完成:
//Define an array of phone numbers
var numbers = ['123-456-7777', '111-222-3333', '963-481-7945'];
//Map takes an array, does something with each element, then returns that result
var sums = numbers.map(function (m) {
//In this case, we return an object containing the original number, and a score
return {
number: m,
//The score is calculated by adding up each number. The match expression creates an array of all terms (g modifier) matching the expression. \d matches a single digit, so we end up with an array of each digit in the number.
//Reduce applies a function to each item in an array, and adds them up
score: m.match(/\d/g).reduce(function (p, c) {
//This looks like magic, but the + before p and c coerces them to numbers (they're strings right now, since match returns an array of strings)
//Both numbers are then added
return +p + +c;
})
}
}).sort(function (a, b) {
//Now that we have the scores of all numbers, we can sort the array to find the highest score
//To be honest, sort() is mostly trial and error for me to find which values to return 1 and -1 for
if (a.score < b.score) return 1;
if (a.score > b.score) return -1;
return 0;
});
//All together, without comments:
sums = numbers.map(function (m) {
return {
number: m,
score: m.match(/\d/g).reduce(function (p, c) {
return +p + +c;
})
}
}).sort(function (a, b) {
if (a.score < b.score) return 1;
if (a.score > b.score) return -1;
return 0;
});
console.log(sums);
document.write("Number with the highest score: " + sums[0].number);
document.write("<br>");
document.write("It's score is " + sums[0].score);
将总和最大的数字打印到控制台。 score
属性.
中返回的对象中也提供数字总和
我正在尝试在 Javascript 中创建一个函数,它将获取数字数组的每个元素(在本例中具体为 phone 数字)并确定哪个元素的总和最高。我已经达到了我感到非常失败的地步,但我认为我已经非常接近了。任何人都可以提供一些指导吗?这是我目前所拥有的:
function highest(inputArray) {
var sum = 0;
var currentHighest = 0;
var largest = 0;
我设置了要使用的变量,然后创建了一个 for 循环来遍历数组中的每个元素。
for (a = 0; a < inputArray.length; a++)
var tempArray = inputArray[a].replace(/\D/g,'');
我创建了一个占位符字符串来删除元素中的所有非整数,然后创建一个函数来对元素的所有数字求和。
function sumDigits(str) {
for (i = 0; i < str.length; i++) {
sum += parseInt(str.charAt(i));
return sum;
}
}
然后创建一个 if 语句来测试当前元素的总和是否大于或等于最高总和元素。
if (sumDigits(tempArray) >= currentHighest) {
currentHighest = sum;
largest = inputArray[a];
return largest;
}
else {
return largest;
}
}
var newArray = ['123-456-7777', '963-481-7945', '111-222-3333'];
console.log(highest(newArray));
这里是整个代码块:
function highest(inputArray) {
var sum = 0;
var currentHighest = 0;
var largest = 0;
for (a = 0; a < inputArray.length; a++)
var tempArray = inputArray[a].replace(/\D/g,'');
function sumDigits(str) {
for (i = 0; i < str.length; i++) {
sum += parseInt(str.charAt(i));
return sum;
}
}
if (sumDigits(tempArray) >= currentHighest) {
currentHighest = sum;
largest = inputArray[a];
return largest;
}
else {
return largest;
}
}
}
var newArray = ['123-456-7777', '963-481-7945', '111-222-3333'];
console.log(highest(newArray));
当我 运行 代码时,如果有帮助,我会得到 "undefined" 作为结果。预先感谢您的帮助。
在您的代码中,您没有在此处初始化 sum
变量,而是在此函数中过早地返回了 sum
值:
function sumDigits(str) {
for (i = 0; i < str.length; i++) {
sum += parseInt(str.charAt(i));
return sum;
}
}
应该是这样的:
function sumDigits(str) {
var sum = 0;
for (i = 0; i < str.length; i++) {
sum += parseInt(str.charAt(i), 10);
}
return sum;
}
如果不在一个块中看到所有代码,我们就无法真正看出还有什么问题,因此我们可以看到不同部分如何相互调用和交互。
这是一个更紧凑的解决方案(假设您尝试对每个 phone 数字中的数字求和):
var phoneNumbers = ["123-456-7890", "982-111-9999"];
var sums = phoneNumbers.map(function(p) {
return p.match(/\d/g).reduce(function(sum, num) {
return sum + parseInt(num, 10);
}, 0);
});
var maxSum = Math.max.apply(Math, sums);
// output results in the snippet window
document.write("sums = " + JSON.stringify(sums) + "<br>");
document.write("maxSum = " + maxSum + "<br>");
工作原理如下:
- 运行
.map()
在 phone 数字数组上,目的是返回一个总和数组。 - 在
.map()
中搜索所有数字,然后在结果数组上 运行.reduce()
累加总和。 - 然后,要获取
sums
数组中的最大值,请使用Math.max()
可以接受整个数组并为您完成最大工作。
如果我对这个问题的解释是正确的(将 phone 数字的每个数字相加,然后打印出最大的结果),你可以这样完成:
//Define an array of phone numbers
var numbers = ['123-456-7777', '111-222-3333', '963-481-7945'];
//Map takes an array, does something with each element, then returns that result
var sums = numbers.map(function (m) {
//In this case, we return an object containing the original number, and a score
return {
number: m,
//The score is calculated by adding up each number. The match expression creates an array of all terms (g modifier) matching the expression. \d matches a single digit, so we end up with an array of each digit in the number.
//Reduce applies a function to each item in an array, and adds them up
score: m.match(/\d/g).reduce(function (p, c) {
//This looks like magic, but the + before p and c coerces them to numbers (they're strings right now, since match returns an array of strings)
//Both numbers are then added
return +p + +c;
})
}
}).sort(function (a, b) {
//Now that we have the scores of all numbers, we can sort the array to find the highest score
//To be honest, sort() is mostly trial and error for me to find which values to return 1 and -1 for
if (a.score < b.score) return 1;
if (a.score > b.score) return -1;
return 0;
});
//All together, without comments:
sums = numbers.map(function (m) {
return {
number: m,
score: m.match(/\d/g).reduce(function (p, c) {
return +p + +c;
})
}
}).sort(function (a, b) {
if (a.score < b.score) return 1;
if (a.score > b.score) return -1;
return 0;
});
console.log(sums);
document.write("Number with the highest score: " + sums[0].number);
document.write("<br>");
document.write("It's score is " + sums[0].score);
将总和最大的数字打印到控制台。 score
属性.