1

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"

1
  • Do you have control over the input? Is that something you wrote? Commented Jun 27, 2022 at 5:41

6 Answers 6

2

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);

Sign up to request clarification or add additional context in comments.

Comments

1

You want to use

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

Note the difference between '' and ' '.

2 Comments

This could easily have been closed as a typo.
This all assumes that OP has control over the input.
0

You can use reduce

inputArr.reduce((prev,curr) => {
    if (curr === '') {
        prev.push('');
    } else {
        prev[prev.length - 1] += curr;
    }
    return prev;
}, [''])

Comments

0

If you have control over the input, You should replace '' with ' ', Then can do the following,

const inputArr = [ 'b', 'a', 'k', 'e', ' ', 'c', 'a', 'k', 'e', ' ', 'e', 'a', 't' ];
const output = inputArr.join('').split(' ');
console.log(output);

IF NOT You can follow this,

const inputArr = [ 'b', 'a', 'k', 'e', '', 'c', 'a', 'k', 'e', '', 'e', 'a', 't' ];
const output = inputArr.map( letter => letter === '' ? ' ':letter).join('').split(' ');
console.log(output);

Comments

0

According to your given array, you want to join the letters and return an array of words, correct me if I am wrong.

So here is my solution for a given problem.

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

let result = inputArr.map(item => item === '' ? ' ' : item).join('').split(' ');

console.log(result);

If you don't have control over input, then you can simply replace '' with ' ' and split it, period.

Comments

0

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

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

    inputArr.forEach((element, index) => {
      if(element === '') {
        inputArr[index] = ' ';
      }
    });
    
const result = inputArr.join('');
    
console.log(result)

1 Comment

["b", "a", "k", "e", " ", "c", "a", "k", "e", " ", "e", "a", "t"].this is not the desired output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.