Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

JavaScript pre-challenge

Hello,

I recently applied to a full-stack immersion program, and as part of the application, I was asked to complete a pre-challenge—to test how I can use resources to work through coding challenges.

The pre-challenge question is: 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'.

I have started working through a solution, but I am having a difficult time!

2 Answers

Here is my implementation of it. It needs refactoring but it works. That was a fun little challenge :-)

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

function findCard (array) {
  // here is where we store our cc numbers after we have removed the dash
  var editedNumbers = [];
 //declare a new array to store the summed values
  var sumTotals = [];
 //remove the dash from numbers and push to our array 
  array.forEach(function(card){
    var str = card.replace(/-/g, "");
    editedNumbers.push(str)
  });
  editedNumbers.forEach(function(c){
    // split cc number into an array and convert to int
    var array = c.split('').map(Number);
    //sum the array 
    var theSum = array.reduce(function(a, b){
        return a + b;
    }, 0);
    // push totals to an array
    sumTotals.push(theSum);
  });
  // find the largest number......
  var maxNumber = Math.max(...sumTotals);
  // and the last index of it
  var index = sumTotals.lastIndexOf(maxNumber);
  return array[index];
}
var ccArray = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'];
console.log(ccArray);

//remove dash in credit card numbers. How do I keep the values create here to use in the rest of my code?

ccArray.forEach(function(entry) {
       var replace = {}
       var replace = entry.replace(/-/g, '');
       console.log(replace);
});

//to add the digits in each CCNumber I need to split the digits into individual values.

var number = 4916260018040530;
var digits = number.toString().split('');
var realDigits = digits.map(Number);
console.log(realDigits);

var sum = realDigits.reduce(function(a, b) { return a + b; }, 0);

console.log(sum);

you need to declare the var replace outside of your forEach function if you want to have access to it elsewhere. You'll want to set it up as an array with [] not {} too. What it looks like you are doing there is declaring an empty object and what you want is an array. Then you want to push the amended card number to it when you have taken the dash out. The code I posted below might help clear it up a bit.