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

208
Views
¿Cómo optimizar el código? (cadena inversa)

La tarea dice: Cambie el orden de las palabras en una oración pero deje que los caracteres no alfanuméricos permanezcan en el mismo lugar, por ejemplo: 'Hola, aquí hay mundo'. -> "aquí, mundo es Hola".

Hasta ahora he creado algo como esto, pero necesito optimizarlo (hay demasiados bucles).

 var text = 'Hello, it is world here.'; function reverseFunc(text){ let alpha = text.match(/[a-z0-9]+/gi).reverse(); let nonAlpha = text.match(/[\W_]+/gi); let result = []; console.log("Alpha : " + alpha) console.log("Non alpha : " +nonAlpha) if(alpha == null || nonAlpha == null) console.log(text) else if((/[a-z0-9]/i).test(text.split("")[0])){ if (alpha.length == nonAlpha.length) { for (let i = 0; i < nonAlpha.length; i++) { result.push(alpha[i], nonAlpha[i]); } console.log(result.join("")) return(result.join("")) }else{ for (let i = 0; i < alpha.length; i++) { result.push(alpha[i], nonAlpha[i]); } console.log(result.join("")) return(result.join("")) } }else{ if (nonAlpha.length == alpha.length) { for (let i = 0; i < alpha.length; i++) { result.push(nonAlpha[i], alpha[i]); } console.log(result.join("")) return(result.join("")) }else{ for (let i = 0; i < nonAlpha.length; i++) { result.push(nonAlpha[i], alpha[i]); } console.log(result.join("")) return(result.join("")) } } } reverseFunc(text)

También tengo un problema cuando el texto tiene solo 1 carácter, por ejemplo, "." o " ". Intenté esto:

 if(alpha == null || nonAlpha == null) console.log(text)

pero parece que solo funciona para caracteres alfanuméricos. ¿Cómo se podría corregir este algoritmo?

about 4 years ago · Juan Pablo Isaza
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!