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 question

Hello, I have recently applied to a full stack immersion program, but am having a difficult time solving the pre-challenge question that they have asked me to answer. I am a beginner when it comes to coding, and I would greatly appreciate it if someone could help explain how to go about solving this question using the JavaScript language. The question is written as follows: "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."

I approached the problem by starting with an array: var myArray = ["720-111-1111","720-999-9999","720-333-3333"];

From what I understood, the code that should follow is something that will remove the "" and - in order to make the phone numbers into usable, addable integers. Then I need the function to find the largest of the three numbers and return, in this case, 720-999-9999. I desperately want to understand what I am doing to answer the question (not interested in copying/pasting and not learning anything). Attending this program is my dream, so I will appreciate any help or direction that I can get!

2 Answers

// STEP ONE: You need to take out dashes: make a regex (Regular Expression) with global modifier g to remove dashes from the telephone number string array.

// STEP TWO: Split each one of these telephone numbers to make array with individual digits so its a two dimensional array now (as a result: [ ["1","2","3","4","5"], ["6","7","8","9"],["10","11","12","13"] ] ).

// STEP THREE: Convert all the Strings that you split in each array to Number primitive type so you can add them all up (preferrably using reduction, its perfect for it).

//STEP FOUR: Associate these numbers with their position in the array somehowe (ideally by index, we do have access to indexes through loops and maps); then assign them to point each other altogether in the proper order; using some sort of a data structure for it (object literal for instance, they resemble dictionaries from other languages very well).

// STEP FIVE: Given we have now an Object structure with summed up numbers as KEYS and their VALUES as telephone numbers , they are bound together, and we can find the target via the biggest number key from an Object.

// LAST STEP: Use the biggest number as an accessor for our Object with telephone numbers.


Here's a simple solution (You may, or may not look at it):

var obj = {},
    lastOne,
    dash = new RegExp("-", 'g'),
    myArray = ["720-111-1111","720-999-9999","720-333-3333"];

var n = myArray
.map(function(e) {
  var result = e.replace(dash, "");
  return result;
})
.map(function(e) {
  var number = e.split("")
  number = number.map(function(e) {
    return Number(e);
  });
  var result = number.reduce(function(a,b,i) {
     return a+b  
  });
return result

});

for(var i = 0; i < myArray.length; i++) {
   obj[n[i]] = myArray[i];
}

lastOne = Object.keys(obj)
.map(function(e) {
   return Number(e);
})
.sort(function(a,b) {
  return a > b;
});

lastOne = obj[lastOne[lastOne.length-1]];
alert(lastOne);

Thank you Maciej! I appreciate that you broke everything down into steps, that was a huge help for me!

Maciej Sitko I like your answer but that is a lot for someone that is just getting into programming to comprehend. Another thing to consider is that the interviewer is looking to see how the interviewee problem solved and how they learn.

Ryan it sounds like you are going down the right path, turning it into smaller steps. I would worry less about the big solution and focus on getting the little things working. This might not be the answer you are looking for but it is better to have an incomplete solution that you can understand than a solution that works and you dont know what is going on.

I had to answer the same question about a year ago and had no idea what I was doing. I submitted an awful function but I could explain what I had learned and I got into the full stack program I am in now.

some resources : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide, http://eloquentjavascript.net/, treehouse, google everything!

To be fair, the interviewer will not always expect candidates to accomplish the task. So I think it is just the health check on the person's state of mind and way of thinking in order to solve the problem. The most important problem is how not to give up.

Thank you Ethan, it was good for me to read your perspective. I wasn't sure if it would look bad if the function wasn't 100%, so I think I've been putting too much pressure on myself. Also, thank you for the helpful links!