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

210
Views
Formateador de números de teléfono usando funciones de constructor JavaScript

Estoy aprendiendo JavaScript y tengo este problema en el que tengo que convertir una matriz de números [6, 5, 0, 8, 3, 5, 9, 1, 7, 2] y

devolver como una cadena en este formato: (650) 835-9172. usando todas las funciones en esta estructura

 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(''); };

¿Alguna ayuda por favor?

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

0

Aquí hay una forma alternativa de usar los literales de plantilla de Javascript

Además, a algunas de sus llamadas a métodos les faltaba this palabra clave, pero otras ya lo han señalado.

 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

Bien, he editado tu fragmento aquí. Le dejo a usted que descubra cómo usar join correctamente para sus requisitos de formato, además de cómo agregar el espacio después del código de área.

 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

se olvidó de usar esta palabra clave para los métodos prototipo de funciones. En cada método de prototipo, ha tomado los elementos cortados como cadenas sin unir los elementos.

Aquí está tu código:-

 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!