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 trialScott 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
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
231,275 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
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
231,275 PointsSteven Parker
231,275 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.
Or watch this video on code formatting.