Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
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?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Here is an alternate way using the Javascript Template Literals

Also some of your method calls were missing the this keyword, but others have already pointed that out.

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

PhoneNumberFormatter.prototype.render = function() {
  return `${this.getAreaCode()} ${this.getExchangeCode()}-${this.getLineNumber()}`;
};

PhoneNumberFormatter.prototype.getAreaCode = function() {
 return `(${this.numbers[0]}${this.numbers[1]}${this.numbers[2]})`;
};

PhoneNumberFormatter.prototype.getExchangeCode = function() {
  return `${this.numbers[3]}${this.numbers[4]}${this.numbers[5]}`;
};

PhoneNumberFormatter.prototype.getLineNumber = function() {
  return `${this.numbers[6]}${this.numbers[7]}${this.numbers[8]}${this.numbers[9]}`;    
};
var pnf = new PhoneNumberFormatter([6, 5, 0, 8, 3, 5, 9, 1, 7, 2]);
console.log(pnf.render());

about 4 years ago · Juan Pablo Isaza Report

0

Okay I have edited your snippet here. I am leaving it to you to figure out how to use join correctly for your formatting requirements, plus how to add the space after the area 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.numbers.slice(0,3);            //I did this
 return this.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('');
};

var pnf = new PhoneNumberFormatter([6, 5, 0, 8, 3, 5, 9, 1, 7, 2]);
console.log(pnf.render());

about 4 years ago · Juan Pablo Isaza Report

0

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())
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!