1

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. 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'

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

4 Answers 4

2

You could write a maxBy function, which takes your function as a parameter for determining the maximum element in the array. 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.

Sign up to request clarification or add additional context in comments.

3 Comments

The code breaks up the CC numbers at the dashes and tries to add those pieces rather than the individual digits. It is never finding the actual digits and never finds the credit card number with the last largest sum of digits. I considered writing a loop to iterate through each credit card number and then use a nested loop to iterate through each character in the string while adding up the ones that are numbers (not dashes). Then finally keep track of the index of the one with the largest sum of digits so that you can return the actual credit card string with the last largest sum.
Did you want to add up the numbers between the dashes instead of adding together the digits? In your example you said the sums were 49, 81, & 64. I can change it by using /-/ instead of /-|/.
Does my code have the wrong output? I'm confused if by "the code" you meant yours or mine.
1

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)

Comments

0

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])

3 Comments

split takes a regex as a parameter, so '-' is the same as /-/. You can simplify the split->join->split to be split(/-|/)
This has the wrong output. It doesn't return the last element that had the max value.
@4castle FSFL will do it ;)
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.