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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Scott Rushford](https://secure.gravatar.com/avatar/2785b92248eb85c2983f570320b1bdc4?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Scott Rushford
1,724 PointsCannot find issue with function for iterating array numbers, adding them, evaluating, and returning largest index.
In this code, I have been able to get to where all items in the array are able to be utilized individually, but I cannot seem to get them to add and evaluate. It appears I am only returning the first number of the array in each sequence. I tried to put many console.log markers to walk it through, but I am baffled.
The goal is to return the actual credit card number with the highest sum of all its digits. Two are the same, but the code must return the most recent. Any ideas to what I am missing?
![Scott Rushford](https://secure.gravatar.com/avatar/2785b92248eb85c2983f570320b1bdc4?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Scott Rushford
1,724 Pointsvar cards =
['5865-2600-5889-0555', '4779-598666-3666', '4252-278553-7978' ,'4556-4242-9283-2260']; // card numbers used console.log(cards, "1"); // raw data
function maxCardSum(card){ // function...returns card when called
var resultMaxSum = 0; // max sum of card to be used
var currentSum = 0; // counter of current card add
var newCards = []; // reassigning modified cards to array
var resultCard;
for (var i = 0; i < cards.length; i++){ // iterate through
newCards[i] = cards[i].replace(/-/g, ""); // R&R dashes with ""
console.log(newCards[i], "2"); // update ... string
newCards[i] = newCards[i].split(""); // split to separate
console.log(newCards[i], "3"); // update now an object
//newCards[i] = newCards[i].toString();
// console.log(typeof newCards[i], "4");
// for loop on the cards
for (var j = 0; j < newCards.length; j++){ // nested loop
console.log(newCards[j], "5"); // currently an object
currentSum += parseInt(newCards[j]); // adding all on string to currentSum ... does not work...
console.log(newCards[j], "6"); // update object
console.log(currentSum, "7"); // update number
} if (resultMaxSum <= currentSum){
resultMaxSum = currentSum; // biggest card updated to this
resultCard = cards[i];} // calculation of max reassigned
}
return resultCard; // returns resultCard as qualifying card
}
maxCardSum(cards); // function maxCardSum, pass in cards
console.log("The credit card number with the max sum of all numbers is: " + resultCard);
1 Answer
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsThe final log statement is attempting to print an internal variable of the maxCardSum function, maybe it was originally inside the function and moved? But now you can capture the returned value and display that instead:
var max = maxCardSum(cards); // capture the returned value in "max"
console.log("The credit card number with the max sum of all numbers is: " + max);
![Scott Rushford](https://secure.gravatar.com/avatar/2785b92248eb85c2983f570320b1bdc4?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Scott Rushford
1,724 PointsThanks Steven! This definitely showed me a pretty big deficit in my code. I was able to make additional changes and it is working well now!!
Steven Parker
230,688 PointsSteven Parker
230,688 PointsWhen posting code, to keep the comments and code separated (and preserve structure in general), use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area.![:arrow_heading_down: :arrow_heading_down:](https://image-proxy-cdn.teamtreehouse.com/3aa59b0d69e23b437da3548709632c5164e6e39c/68747470733a2f2f7374617469632e7465616d74726565686f7573652e636f6d2f696d616765732f656d6f6a692f756e69636f64652f323933352e706e67)
Or watch this video on code formatting.