Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda