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

134
Views
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 answers
Answer question

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 Report

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 Report

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 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!