简体   繁体   English

如何找到数组中存储的字符串中的数字之和

[英]How to find sum of numbers in string stored in array

I have figured out how to calculate the value of numbers from a single string, using as an example.. 我已经举例说明了如何从单个字符串计算数字的值。

var sum = "13-2-10-7-3".split('-').reduce(function(x, y) {
    return parseInt(x)+ parseInt(y);
}); // Value of 35

Im interested in finding the credit card number whose digits sum to the largest number. 我有兴趣查找数字总和最大的信用卡号。 If more than one has the same largest sum of digits, we want the last one in the list with that sum. 如果多个具有相同的最大数字总和,我们希望列表中的最后一位具有该总和。

Here is a sample array of credit card numbers: 这是信用卡号码的示例数组:

['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'] 

In the sample array above, the digits add up to 49, 81, 81, and 64 respectively. 在上面的示例数组中,数字分别总计为49、81、81和64。 Since there are two which have the same sum, the function should return the last one with that sum, in this case '4252-278893-7978' 由于存在两个具有相同总和的函数,该函数应返回具有该总和的最后一个,在这种情况下为'4252-278893-7978'

I am stuck trying to figure out how to now apply this to an array of numbers.. 我一直试图找出如何将其应用于数字数组的方法。

  1. Contain all variables and code needed within a function. 包含函数中所需的所有变量和代码。
  2. Have that function take one argument which will be an array of credit card number strings. 让该函数接受一个参数,它将是信用卡号字符串的数组。
  3. Determine the sum of digits for each credit card number. 确定每个信用卡号的数字总和。
  4. Determine which credit card number has the last largest sum of digits. 确定哪个信用卡号的数字总和最大。
  5. Use a return statement to return the required card number in its' original form 使用退货声明以其原始形式退还所需的卡号

Insight would be much appreciated 洞察力将不胜感激

You could write a maxBy function, which takes your function as a parameter for determining the maximum element in the array. 您可以编写一个maxBy函数,该函数将您的函数用作确定数组中最大元素的参数。 This would let you easily adapt your code without much work. 这样一来,您无需进行大量工作即可轻松调整代码。

 var cards = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260']; function maxBy(arr, func) { return arr.reduce(function(max, val) { return func(val) >= func(max) ? val : max; }, arr[0]); } function sumCC(card) { return card.split(/-|/).reduce(function(sum, val) { return sum + parseInt(val, 10); }, 0); } console.log(maxBy(cards, sumCC)); 

This is a useful utility function. 这是一个有用的实用程序功能。 The Lodash utility library also provides a _.maxBy . Lodash实用程序库还提供了_.maxBy

Loop over the credit card numbers and check if sum is max store it and continue to loop and store the maximum credit card number and return it: 循环检查信用卡号并检查总和是否最大,然后继续循环并存储最大信用卡号并返回:

function max_credit_sum(arr){
  max = -1
  credit_card = ''
  for(ele of arr){
    numbers = ele.replace(/-/g, '').split('')
    sum = 0
    for(num of numbers){
      sum += parseInt(num)
    }
    if(sum >= max){
      credit_card = ele
      max = sum
    }
  }
 return credit_card
}

// Test
arr = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'] 
maximum = max_credit_sum(arr)

Its not the most performant, but its simple and quick 它不是性能最高的,但简单快捷

 var ccs = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'] // FSFL = F*Simple For-Loop var sum = (a, b) => a + b; var largestSum = 0; var largestSumLastIdx = 0; for (let i = 0; i < ccs.length; i++) { let s = ccs[i].split(/-|/).map(Number).reduce(sum, 0); if (s >= largestSum) { largestSum = s; largestSumLastIdx = i; } } console.log("The winner is: " + ccs[largestSumLastIdx]) 

You might get the card with maximum sum like this; 您可能会这样获得最大金额的卡;

 var cards = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'], cardSums = cards.map(c => c.split("-").reduce((p,c) => +p + +c)), maxCard = cards[cardSums.indexOf(Math.max(...cardSums))]; console.log(maxCard); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM