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

262
Vistas
Codewars: how can i solve this codewar question?

I have attempted this so many times even using regex and the len, left, mid and right methods and i cant seem to get it right. The goal is this aaa33aa4a7a6 ==> aaa33aa-a-a-

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could check if there comes at least two or less digits in the string and replace the digits.

const
    regex = /\d(?=(\D*\d){0,2}\D*$)/g;

console.log('abc12aa3b9c8');
console.log('abc12aa3b9c8'.replace(regex, '-'));

console.log('abc12aa3b9c8x');
console.log('abc12aa3b9c8x'.replace(regex, '-'));

about 4 years ago · Juan Pablo Isaza Denunciar

0

I convert the string to an array the reverse it. Afterwards i iterate this new array and create a new array by condition (count the matches of numbers).

const str = `abc12aa3b9c8`
let arr = str.split("").reverse(); 

let counter = 0;
let newStr = []

arr.forEach(e => {  
  if (isNaN(e)) {       
    newStr.push(e)
  } else {    
    newStr.push(counter < 2 ? '-' : e)         
    counter++;
  }
})


console.log('res',newStr.reverse().join(""))

about 4 years ago · Juan Pablo Isaza Denunciar

0

Split your string to array, reverse, replace what you need, reverse it back, join into a result string:

    var str = 'abc12aa3b9c8';
    var counter = 3;
    str = str
      .split('')
      .reverse()
      .map(function(char){
        if( !isNaN( char ) && counter-- > 0 )
          return '-';
        return char
      })
      .reverse()
      .join('');
    console.log( str );
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