0

I've been tasked with writing a function called "getAllWords".

Given a sentence, "getAllWords" returns an array containing every word in the sentence.

Notes: * If given an empty string, it should return an empty array.


What I've come up with is:

function getAllWords(str) {

 var array = [];
  for (var i = 0; i < str.length; i++) {
     array.push(str[i]);
   }
   return array;
   }

   getAllWords('Radagast the Brown');

What I get is:

  [ 'R','a','d','a','g','a','s','t',' ','t','h','e',' ','B','r','o','w','n']

but what I'm trying for is:

   ['Radagast', 'the', 'Brown']

6 Answers6

-2

You can use the split method for this purpose:

function getAllWords(sentence) {
  return typeof sentence === 'string' && sentence.length > 0 ?
         sentence.split(' ') : [];
}

var words = getAllWords('Radagast the Brown');

We then check if the input exists, is actually a string and has any content before returning the split version or an empty array.

Assuming you will always get a string as input you can further simplify it to:

function getAllWords(sentence) {
  return sentence.length > 0 ? sentence.split(' ') : [];
}

var words = getAllWords('Radagast the Brown');
Gorka Hernandez
  • 3,890
  • 23
  • 29
  • 1
    `sentence && typeof sentence === 'string' && sentence.length > 0` is way overkill. – Ry- Jul 20 '17 at 07:27
  • You are absolutely right about the `sentence && typeof sentence === 'string'` part as it's plain redundant. I would still make sure that the input is a valid string and has content unless we can assume that the input will always be a string. Updated the answer to reflect the change. – Gorka Hernandez Jul 20 '17 at 10:09
  • 1
    @GorkaHernandez thank you Gorka! your answer worked because it accounted for if the string is empty. :-) – Melvin Mullings Jul 20 '17 at 22:00
-3

As you can see, iterating over a string will get you each character.

To get each words, you should use String.split(' ')

function getAllWords(str) {
  return str.split(' ');
}

console.log(getAllWords('Radagast the Brown'));
Weedoze
  • 13,683
  • 1
  • 33
  • 63
-3

var sentence = 'Radagast the Brown';
var words = sentence.split(' ').filter(c => c != '');

document.write(JSON.stringify(words));

This is the code you need.

Daniel Tran
  • 6,083
  • 12
  • 25
-3

You just need to use the .split() function. This will automatically convert the results into an array:

function getAllWords(str) {
    if(str === "") {
        var array = {};
    } else {
        var array = str.split(" ");
    }
    return array;
}

EDIT: The function also checks whether the string is empty or not and returns an empty array if the string is empty.

Jeff G
  • 122
  • 1
  • 6
-3

Use javascripts String split() function.

For Example :

var str = "Radagast the Brown";
var arr = str.split(' ');
console.log(arr);
Mahesh Hegde
  • 184
  • 1
  • 4
-3

function getAllWords(str) {

 var arr = str.split(" ")
 return arr;
}

  console.log(getAllWords('Radagast the Brown'));
Kay Kap
  • 11
  • 5