-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitify.js
More file actions
37 lines (26 loc) · 1.42 KB
/
splitify.js
File metadata and controls
37 lines (26 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// *** Functional Programming: Split a String into an Array Using the split Method ***
// The split method splits a string into an array of strings. It takes an argument
// for the delimiter, which can be a character to use to break up the string or a
// regular expression. For example, if the delimiter is a space, you get an array
// of words, and if the delimiter is an empty string, you get an array of each
// character in the string.
// Here are two examples that split one string by spaces, then another by digits
// using a regular expression:
// var str = "Hello World";
// var bySpace = str.split(" ");
// Sets bySpace to ["Hello", "World"]
// var otherString = "How9are7you2today";
// var byDigits = otherString.split(/\d/);
// Sets byDigits to ["How", "are", "you", "today"]
// Since strings are immutable, the split method makes it easier to work with them.
// # Use the split method inside the splitify function to split str into an array
// of words. The function should return the array. Note that the words are not
// always separated by spaces, and the array should not contain punctuation.
function splitify(str) {
// Add your code below this line
// replace(/[\. , -]+/g, " ") is a regexp that replaces every '.', ',' or '-' with an empty space
const regex
return str.replace(/[\. , -]+/g, " ").split(' ');
// Add your code above this line
}
splitify("Hello World,I-am code");