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
Ryan Roybal
4,682 PointsI am beyond stuck.
I am working on a Pre-Challenge for a bootcamp I have applied for and have been working on learning how to solve the question for some time. i'm not sure if i'm approaching the problem correctly but this is what i have so far.
Challenge:
For this challenge, we are interested in finding the phone number whose digits sum to the largest number. Write a single function that outputs the “largest” phone number in an input array (if multiple numbers are equally large, return the last one). Assume that the phone numbers are strings of 10 digits separated by dashes. input array ['123-456-7777', '963-481-7945', '111-222-3333']
// ACTUAL PROJECT
//the array of phone numbers
var arr = [ '123-456-7777', '963-481-7945', '111-222-3333' ];
console.log(arr)
// function in which takes strings with heifens and removes heifens and turns the strings into usable integers
arr.forEach(function(entry) {
var ints = {}
var ints = entry.replace(/-/g, "");
console.log(ints);
});
//I can't seem to use the ints in the next function to output the largest number.
// function that takes the array and calculates and returns the largest (sum)
var largest = arr.reduce(function(x,y){
return (x > y) ? x : y;
});
console.log(largest);
//it outputs what i need but i want it to work with the integers in the 1st function. And also the challenge asks for a single function. how would i combine these. i'm guessing a for loop of some sort. but am stuck here.
any help would be greatly appreciated. even just a hint on what i should learn that may spark the light in my head that has grown dim.
3 Answers
Ali M Malik
33,293 PointsHope this helps.
Jeffery Austin
8,128 PointsThis code below that you are using is going through and setting the var ints to empty each time through the forEach loop then storing the current index of arr into the var ints then console logging the current value. I'm assuming you want var ints to store all the values. If that is the case you don't want your forEach loop setting var ints to empty each time.
arr.forEach(function(entry) {
var ints = {}
var ints = entry.replace(/-/g, "");
console.log(ints);
});
Jacob Dobson
5,122 PointsRyan Roybal did you ever find this out? I have the same question and I'm having a hell of a hard time here. Is there something that helped you get through it, did you even finish it? Not sure, how you get to each number in the phone number strings in order to add them up!