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

136
Visualizações
convert array of letters to array of words using JavaScript

convert array of letters to array of words using JavaScript

const inputArr = [ 'b', 'a', 'k', 'e', '', 'c', 'a', 'k', 'e', '', 'e', 'a', 't' ]; and my desired output is : output = ['bake','cake','eat'];

I used Join method but I am not getting desired output. const inputArr = [ 'b', 'a', 'k', 'e', '', 'c', 'a', 'k', 'e', '', 'e', 'a', 't' ];

    const result=inputArr.join('');
    console.log(result);

output: "bakecakeeat"

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

If you want the answer in different way other than Join.

You can iterate over the inputArr then keep appending the character until you found the empty space(''), there you can add the word in the outputArr and and reset the word.

const inputArr = [ 'b', 'a', 'k', 'e', '', 'c', 'a', 'k', 'e', '', 'e', 'a', 't' ];
var outputArr = [];
var word = '';
// Iterate over the array.
for (let i = 0; i < inputArr.length; i++) {
  if(inputArr[i]=='') {
    // If you found the empty Space(''), add to the outputArr
    outputArr.push(word);
    // reset the word, to start making the new word.
    word = '';
    // No need to execute the below code, so continue.
    continue;
  }
  // Keep appending the character to the word.
  word = word.concat(inputArr[i]);
}
// This is for the last word string.
if(word != '') {
 outputArr.push(word);
}
console.log(outputArr);

about 4 years ago · Juan Pablo Isaza Relatório

0

You want to use

const inputArr = [ 'b', 'a', 'k', 'e', ' ', 'c', 'a', 'k', 'e', ' ', 'e', 'a', 't' ];

Note the difference between '' and ' '.

about 4 years ago · Juan Pablo Isaza Relatório

0

If you do not have control over the input you can change the array to fit the other answer's method:

inputArr.forEach((element, index) => {
  if(element === '') {
    inputArr[index] = ' ';
  }
});
inputArr.join('');
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