freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/combine-an-array-into-a-string-using-the-join-method.english.md
Oliver Eyton-Williams bd68b70f3d
Feat: hide blocks not challenges (#39504)
* fix: remove isHidden flag from frontmatter

* fix: add isUpcomingChange

Co-authored-by: Ahmad Abdolsaheb <ahmad.abdolsaheb@gmail.com>

* feat: hide blocks not challenges

Co-authored-by: Ahmad Abdolsaheb <ahmad.abdolsaheb@gmail.com>

Co-authored-by: Ahmad Abdolsaheb <ahmad.abdolsaheb@gmail.com>
2020-09-03 15:07:40 -07:00

2.4 KiB

id, title, challengeType, forumTopicId
id title challengeType forumTopicId
587d7daa367417b2b2512b6c Combine an Array into a String Using the join Method 1 18221

Description

The join method is used to join the elements of an array together to create a string. It takes an argument for the delimiter that is used to separate the array elements in the string. Here's an example:
var arr = ["Hello", "World"];
var str = arr.join(" ");
// Sets str to "Hello World"

Instructions

Use the join method (among others) inside the sentensify function to make a sentence from the words in the string str. The function should return a string. For example, "I-like-Star-Wars" would be converted to "I like Star Wars". For this challenge, do not use the replace method.

Tests

tests:
  - text: Your code should use the <code>join</code> method.
    testString: assert(code.match(/\.join/g));
  - text: Your code should not use the <code>replace</code> method.
    testString: assert(!code.match(/\.?[\s\S]*?replace/g));
  - text: <code>sentensify("May-the-force-be-with-you")</code> should return a string.
    testString: assert(typeof sentensify("May-the-force-be-with-you") === "string");
  - text: <code>sentensify("May-the-force-be-with-you")</code> should return <code>"May the force be with you"</code>.
    testString: assert(sentensify("May-the-force-be-with-you") === "May the force be with you");
  - text: <code>sentensify("The.force.is.strong.with.this.one")</code> should return <code>"The force is strong with this one"</code>.
    testString: assert(sentensify("The.force.is.strong.with.this.one") === "The force is strong with this one");
  - text: <code>sentensify("There,has,been,an,awakening")</code> should return <code>"There has been an awakening"</code>.
    testString: assert(sentensify("There,has,been,an,awakening") === "There has been an awakening");

Challenge Seed

function sentensify(str) {
  // Only change code below this line


  // Only change code above this line
}
sentensify("May-the-force-be-with-you");

Solution

function sentensify(str) {
  // Only change code below this line
  return str.split(/\W/).join(' ');
  // Only change code above this line
}