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

126
Views
La función no está extrayendo todo el valor de la matriz en mecanografiado

El programa tiene que juntar todas las letras pero no funciona. He probado con el depurador pero no muestra ningún error. El código:

 var phrase = [ 'h', 'e', 'l', 'l', 'o',' ', 'w', 'o', 'r', 'l', 'd' ]; let sentence: string[] = createSentence(phrase); sentence.forEach(letter => { console.log(letter); }); function createSentence(letters: string[]): string[] { var add = ""; var result: string[] = []; phrase.forEach(unionL => { if (unionL != ' ') { add += unionL; } else { result.push(add); add = ""; } }); return result; }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No está extrayendo toda la matriz debido a la condición que ha utilizado en su matriz. Debe agregar una condición más para que el último fragmento se agregue a la matriz de resultados. Aquí está el código:

 function createSentence(letters: string[]): string[] { var add = ""; var result: string[] = []; phrase.forEach((unionL,index) => { if (unionL != ' ') { add += unionL; } else { result.push(add); add = ""; } //New Code if(index===phrase.length-1){ result.push(add); } }); return result; }

Otra forma es mantener su código y simplemente agregar una cadena vacía al final de la cadena que funcionará con su lógica. Ejemplo:

 var phrase = [ 'h', 'e', 'l', 'l', ' ', 'w', 'o', 'r', 'l', 'd', ' ' ];
about 4 years ago · Juan Pablo Isaza Report
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!