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

170
Vistas
Check if character in string is an integer, and if so grab that int

I am currently on the codewars site trying to improve my JavaScript along with other things. This question will give an input like this 'is2 Thi1s T4est 3a' and expect an output like this 'Thi1s is2 3a T4est'

I am having trouble indexing the string. It seems odd as I've done it before in Python but even trying this in Python I don't get the proper output.

Here is my code, before it was giving me at least an array of the numbers but now I just get [ Array(4) ].

I know that the proper output should be a string but once I get it all in an array I plan to just tempSentence.join(" ") the array and return it. Sorry that is kind of messy but I kept trying different things:

function order(words){
    let hasNumber = /\d/;
    let newSentence = words.split(" ")
    let tempSentence = words.split(" ")

    for (let i = 0; i < newSentence.length; i++){
      for (let j = 0; j < newSentence[i].length; j++){
        if (hasNumber.test(newSentence[i][j])){
          tempSentence[words[i][j]-1] = newSentence[i]
        }
      }
    }
    return tempSentence
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Creating two separate arrays and trying to rearrange indicies in a nested loop seems quite convoluted for what you're trying to accomplish. An easier approach would be to take just one array of words, and .sort it by having a helper function that extracts the digit from it, and comparing the extracted number of the two elements being compared inside the callback.

const getNum = word => word.match(/\d+/)[0];
const order = (words) => words
  .split(' ')
  .sort((a, b) => getNum(a) - getNum(b))
  .join(' ');
console.log(order('is2 Thi1s T4est 3a'));

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