* fix: move "Explore Differences Between..." to basic JS, update seed and tests * fix: resequence "Declare String Variables" * fix: move "Declare a Read-Only Variable..." to basic JS, update seed and tests * fix: revert changes to non-English "Explore Differences Between..." test text * fix: revert test strings, solutions, and seeds for non-English challenges * fix: update "Declare String Variables" description * fix: sync quotation marks in description and seed * fix: modify note in "Declare a Read-Only..." challenge * fix: update operator and compound assignment challenges * fix: update string challenges * fix: update array and array method challenges * fix: update function and scope challenges, resequence slightly * fix: "Word Blanks" solution * fix: add spacing to seed * fix: concatenating += challenge spacing * fix: appending variables to strings spacing * fix: find the length of a string spacing * fix: removed instances of removedFromMyArray = 0 * fix: switch challenges * fix: function argument and param spacing * fix: update counting cards, object challenges, and record collection * fix: finish rest of Basic JS section * fix: introducing else statements solution * fix: update spacing and wording * fix: update wording for const challenge * fix: update functional programming challenges * fix: intermediate algorithms and cert challenges * fix: revert some spacing and remove comments for fp challenge solutions * feat: add notes with links to moved let and const challenges in first two es6 challenges * fix: update es6 intro text * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-the-plus-equals-operator.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/finding-a-remainder-in-javascript.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/global-scope-and-functions.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/declare-a-read-only-variable-with-the-const-keyword.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * fix: concatenating strings with plus operator seed * fix: add comments back to Declare a Read-Only Variable... seed * feat: add es6 to basic javascript redirect tests for let and const challenges * fix: revert "Concatenating Strings with Plus Operator" seed * fix: move test file to cypress/integration/learn/redirects, separate redirect tests Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
88 lines
2.2 KiB
Markdown
88 lines
2.2 KiB
Markdown
---
|
|
id: 587d7daa367417b2b2512b6b
|
|
title: Split a String into an Array Using the split Method
|
|
challengeType: 1
|
|
forumTopicId: 18305
|
|
dashedName: split-a-string-into-an-array-using-the-split-method
|
|
---
|
|
|
|
# --description--
|
|
|
|
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:
|
|
|
|
```js
|
|
const str = "Hello World";
|
|
const bySpace = str.split(" ");
|
|
|
|
const otherString = "How9are7you2today";
|
|
const byDigits = otherString.split(/\d/);
|
|
```
|
|
|
|
`bySpace` would have the value `["Hello", "World"]` and `byDigits` would have the value `["How", "are", "you", "today"]`.
|
|
|
|
Since strings are immutable, the `split` method makes it easier to work with them.
|
|
|
|
# --instructions--
|
|
|
|
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.
|
|
|
|
# --hints--
|
|
|
|
Your code should use the `split` method.
|
|
|
|
```js
|
|
assert(code.match(/\.split/g));
|
|
```
|
|
|
|
`splitify("Hello World,I-am code")` should return `["Hello", "World", "I", "am", "code"]`.
|
|
|
|
```js
|
|
assert(
|
|
JSON.stringify(splitify('Hello World,I-am code')) ===
|
|
JSON.stringify(['Hello', 'World', 'I', 'am', 'code'])
|
|
);
|
|
```
|
|
|
|
`splitify("Earth-is-our home")` should return `["Earth", "is", "our", "home"]`.
|
|
|
|
```js
|
|
assert(
|
|
JSON.stringify(splitify('Earth-is-our home')) ===
|
|
JSON.stringify(['Earth', 'is', 'our', 'home'])
|
|
);
|
|
```
|
|
|
|
`splitify("This.is.a-sentence")` should return `["This", "is", "a", "sentence"]`.
|
|
|
|
```js
|
|
assert(
|
|
JSON.stringify(splitify('This.is.a-sentence')) ===
|
|
JSON.stringify(['This', 'is', 'a', 'sentence'])
|
|
);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
function splitify(str) {
|
|
// Only change code below this line
|
|
|
|
|
|
// Only change code above this line
|
|
}
|
|
|
|
splitify("Hello World,I-am code");
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
function splitify(str) {
|
|
return str.split(/\W/);
|
|
}
|
|
```
|