Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

265
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda