Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PhoneNumber Formatter using Constructor functions JavaScript

I’m learning JavaScript, and I have this problem where I have to convert an array of numbers [6, 5, 0, 8, 3, 5, 9, 1, 7, 2] and

return as a string in this format: (650) 835-9172. using all the functions in this structure

function PhoneNumberFormatter(numbers) {
  this.numbers = numbers;
}

PhoneNumberFormatter.prototype.render = function() {
  var string = '';
  string = getAreaCode()+getExchangeCode()+"-"+getLineNumber()    //I did this
 
  return string;
};

PhoneNumberFormatter.prototype.getAreaCode = function() {
 let areaCode = this.numbers.slice(0,3);            //I did this
 return parenthesize(areaCode)                      //I did this

};

PhoneNumberFormatter.prototype.getExchangeCode = function() {
  return this.numbers.slice(3,6);     //I did this
 
};

PhoneNumberFormatter.prototype.getLineNumber = function() {
  return this.numbers.slice(6,10);     //I did this
};

PhoneNumberFormatter.prototype.parenthesize = function(string) {
  return '(' + string + ')';
};

PhoneNumberFormatter.prototype.slice = function(start, end) {
  return this.numbers.slice(start, end).join('');
};

Some help please?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you have forgot to use this keyword for the functions prototype methods. In each prototype method you have taken the sliced elements as strings without joining the elements.

Here is your code:-

function PhoneNumberFormatter(numbers) {
  this.numbers = numbers;
}

PhoneNumberFormatter.prototype.render = function() {
  var string = '';
  string = this.getAreaCode()+this.getExchangeCode()+"-"+this.getLineNumber()    //I did this
 
  return string;
};

PhoneNumberFormatter.prototype.getAreaCode = function() {
 let areaCode = this.slice(0,3);//I did this
 return this.parenthesize(areaCode)                      //I did this

};

PhoneNumberFormatter.prototype.getExchangeCode = function() {
  return this.slice(3,6);     //I did this
 
};

PhoneNumberFormatter.prototype.getLineNumber = function() {
  return this.slice(6,10);     //I did this
};

PhoneNumberFormatter.prototype.parenthesize = function(string) {
  return '(' + string + ')';
};

PhoneNumberFormatter.prototype.slice = function(start, end) {
  return this.numbers.slice(start, end).join('');
};

console.log(new PhoneNumberFormatter([6, 5, 0, 8, 3, 5, 9, 1, 7, 2]).render())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading