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
Colin Stell
Front End Web Development Techdegree Graduate 25,702 PointsHelp with my JavaScript Code
I am working on a technical exercise for a bootcamp admissions process.
"You will be using JavaScript to write a simple function that can accomplish a goal.
We want to find 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 been working through the code and I know the following code doesn't work, but I am looking for some feedback as I continue to work through this.
var creditArray = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'];
function largestCardSum(arrayArg) {
while(i = 0; i < arrayArg.length; i++)
{arrayArg[i]; replace(/-/; ' ');}
{parseInt(arrayArg);}
{arrayArg.toString().split('');}
{Math.max(arrayArg);}
return arrayArg.largestIndexof;
}
largestCardSum(creditArray);
6 Answers
Steven Parker
243,134 PointsI see a few issues at first glance.
- a while would only take 1 expression, but you have three like a for loop would use
- you have braces around individual statements that are not part of a loop or condition (not illegal, but odd)
- most of your "statements" are just expressions which are never assigned to any variable
- by themselves, expressions don't do anything
Thomas Nilsen
14,957 PointsJason Anello
Courses Plus Student 94,610 PointsPosting a solution isn't going to help them when they get further into this process and can't ask others for help.
Also, you've made the solution more complicated than it needed to be and there's a few bugs.
If you look at the problem statement, the sum of the first credit card number is 49, not 9850 as you have in your comments. You have to add up the individual digits in each card number.The sum could never be more than 144 and that is if your card was all 9's
You can't use the sort method for this. the sort method isn't guaranteed to be stable which means it won't necessarily preserve the original order of equal items. If 2 credit cards had the same sum as in the example, you can't be sure that the one that came later will be the first one in the sorted array.
Colin Stell
Front End Web Development Techdegree Graduate 25,702 PointsSteven & Thomas,
Thank you guys for your input. Really my code is the thought process of steps and I definitely realize that it's not final. You're input and feedback is greatly appreciated. I'll take both of your thoughts and head back to the drawing board, so to speak.
Jason, agreed. I definitely didn't want the answer, just feedback.
Thomas Nilsen
14,957 PointsI updated my answer.
Jason Anello
Courses Plus Student 94,610 PointsHi Colin Stell ,
Not sure how far along you are but I would recommend against using any functions like Math.max or anything else that would do the work for you. I would guess that it would be seen as a negative. For something like this they probably want to see how you solve problems and not so much that there's a function you can call that does the work for you.
It's probably ok to use something like parseInt() but I wouldn't recommend much beyond that.
Thomas Nilsen
14,957 PointsI'm not sure if I agree.
By using methods (from for example Math), he demonstrates that he is familiar with the language, what it offers and when to use them.
So unless the exercise explicitly says to avoid certain helper-methods I don't see why he shouldn't
Jason Anello
Courses Plus Student 94,610 PointsWe can only guess on what they're looking for but I think it's valuable to be able to show that you can find a maximum value yourself or you can sum a bunch of numbers by keeping a running total. I think those 2 are the key things in solving this problem.
Calling all these functions and coming up with a slower solution is going to show that you're not thinking about performance, I think.
I think that the problem is constructed in a way to discourage the use of some of these functions. It wants the last credit card number with the max value which is going to rule out certain solutions involving built-in functions.
Also, you're still suggesting the sort() method which I don't see how it's going to lead to a correct solution in this case.
Thomas Nilsen
14,957 PointsJason Anello
Courses Plus Student 94,610 PointsThat really seems like a lot of function calls for a problem like this.
Your solution is dependent upon the sort being stable. This is what I tried to talk about in my first comment to your original answer.
A stable sort will preserve the original order of equal items. In this case, that 2nd and 3rd credit card had the same maximum sum of 81.
You're sorting largest sum first so a stable sort would put that 2nd card at index 0 and the 3rd card will be at index 1.
With an unstable sort, those 2 cards will still be at index 0 and 1 but not necessarily in their original order with respect to each other. So in others words, if you have multiple maximum values, they will all be put together at one end of the array but you don't know what order they will be in with respect to each other.
javascript's sort method is not guaranteed to be stable so you can't write code that depends on that.
Try the following 11 card numbers with your code in the pastebin:
var creditCards = [
'4916-2600-1804-0530',
'4916-2600-1802-0530',
'4779-252888-3972',
'4556-4242-9783-2260',
'4916-2601-1804-0530',
'4252-278893-7978', // correct
'4556-4242-9283-2260',
'4916-2600-1804-3530',
'4916-2600-1804-9530',
'4916-2300-1804-0530',
'4556-4242-9283-2160'
];
It still contains the original 2nd and 3rd card numbers that had the maximum sum of 81. The rest of the numbers added in all have smaller sums. So you're still looking for the same credit card as before.
Try it in both firefox and chrome. You'll find that firefox still gives the correct card number but chrome gives the other number that had the sum of 81. This is because chrome's sort is unstable for arrays with more than 10 elements.
Thomas Nilsen
14,957 PointsYeah - your right.
Check out this approach instead
That works with your example as well.
I'm not saying this is efficient, but for small tasks such as this one I really like it.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsIf leaving an answer here, please don't give out the solution considering that this is a technical exercise for an admissions process.
Hints are ok.