-1

New to JavaScript. below is my code, below that is what the algorithm is suppose to accomplish

function creditSum(arr) {
    var num_of_elements = parseInt(arr)
    array = arr.toString().split()
    array = array.map(Number)
    var sum_of_array = 0 
    for (var i = 0; i < num_of_elements; i++){
        sum_of_array += array[i]
    }

    return sum_of_array 
}
console.log(creditSum(['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260']))

For this challenge, we are 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. Write a single function that takes one argument. That argument will be an array of credit card numbers. Assume the array can have any number of credit card numbers and each one is a string of digits and dashes. Your function should return the credit card number that has the largest sum of digits.

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'

5
  • Is this an interview question you are looking for someone to solve for you? Commented Jan 16, 2017 at 21:07
  • There are a number of things wrong here. parseInt is not going to give you the number of elements. That would be arr.length. There's no reason to convert the array to a string then immediately back into an array. Mapping to Number is not going to do what you think it will do. You also aren't doing anything to track the largest sum. Commented Jan 16, 2017 at 21:07
  • 1
    That's some awesome homework you got there. What's your problem? Commented Jan 16, 2017 at 21:07
  • No its not for an interview lol, just a challenge that I couldn't quite figure out. @Dekel Commented Jan 16, 2017 at 21:43
  • @MikeC, thank you. I knew I was forgetting some key steps, I had a max function in my previous attempt before posting this. Just posted it slightly broken down. Commented Jan 16, 2017 at 21:45

2 Answers 2

0

Primitive way in doing this. Just for the sake of learning. I hope this gives you a clear understanding.

function creditSum(arr) {
    var sum = [], current = 0, last = 0, w = 0;

    //remove the dashed from the array item
    for(var i = 0; i < arr.length; i++){
    	var newarr = arr[i].toString().split('-');
    	var x = newarr.length;
        var s = 0;

        //loop in from the new array of item after the split
        for(var y = 0; y < x ; y++){
            var ss = 0;
            
            //get the sum of each digit
            for(var z = 0 ; z < newarr[y].length; z++){
            	ss += parseInt(newarr[y][z]);
            }
            s += parseInt(ss);
        }
        
        //push it inside an array of sum
        sum.push(s);
    }
    
    //check highest sum
    for(var d = 0; d < sum.length; d++){
    	if(d == 0) { last = sum[0] };
        current = sum[d];
        if(last <= current){
            w = d;
            last = current;
        }
        
    }
    
    return arr[w] 
}
document.body.innerHTML = (creditSum(['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260']));

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

1 Comment

Thank you. I would give you a like if I had the rep for it.
0

I think that this should do it:

function creditSum(arr) {
  const resultIndex = arr
   .map(x => x
     .split('')
     .map(x => parseInt(x))
     .filter(x => !isNaN(x))
     .reduce((total, current) => total + current, 0)
   )
   .map((total, idx) => ({ idx, total }))
   .sort((a, b) => b.total - a.total)
   .reduce((result, current) =>
     current.total >= result.total ? current : result,
     { total: 0 }
   )
   .idx;

  return arr[resultIndex];
}

1 Comment

thank you. the code does work properly. The way you solved this is very interesting to me using the const function. I am looking forward to breaking this down as it is different than what I am used to seeing.

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.