fork(1) download
  1. function getNumber(s) {
  2. for (const char of s) {
  3. let n = parseInt(char);
  4. if (! isNaN(n)) { // se é número, retorna
  5. return n;
  6. }
  7. }
  8. // estou assumindo que sempre terá um número na string, então aqui não retorno nada
  9. }
  10.  
  11. function order(frase) {
  12. if (frase.length == 0){
  13. return '';
  14. }
  15. let palavras = frase.split(' ');
  16. let result = [];
  17. for (const s of palavras) {
  18. result[getNumber(s) - 1] = s;
  19. }
  20. return result.join(' ');
  21. }
  22.  
  23. console.log(order("4of Fo1r pe6ople g3ood th5e the2"));
  24.  
Success #stdin #stdout 0.04s 16636KB
stdin
Standard input is empty
stdout
Fo1r the2 g3ood 4of th5e pe6ople