Truncate Sentence Lab
In this lab, you will be given a sentence s
and an integer k
. Your task is to truncate the sentence s
such that it contains only the first k
words. To achieve this, you will implement a function called truncateSentence
, which will take two arguments - a string s
, representing the sentence, and a number k
, representing the number of words to truncate the sentence to.
This lab will test your understanding of string manipulation and basic knowledge of JavaScript.
Requirements
- Use ESM import/export everywhere.
- Follow the instructions given in the lab carefully.
- Make sure to export any function, variable, etc. that you will be using in the Evaluation Script.
Examples
Here are a few examples to help you understand the problem better:
Example 1:
truncateSentence("Hello how are you Contestant", 4);
Output: "Hello how are you"
Explanation:
The words in s
are ["Hello", "how", "are", "you", "Contestant"].
The first 4 words are ["Hello", "how", "are", "you"].
Hence, you should return "Hello how are you".
Example 2:
truncateSentence("What is the solution to this problem", 4);
Output: "What is the solution"
Explanation:
The words in s
are ["What", "is", "the", "solution", "to", "this", "problem"].
The first 4 words are ["What", "is", "the", "solution"].
Hence, you should return "What is the solution".
Example 3:
truncateSentence("chopper is not a tanuki", 5);
Output: "chopper is not a tanuki"
Challenges
- Ensure you read the input string carefully.
- Split the input string by space and extract the first
k
words. - Join the extracted words to form a truncated sentence.
- Return the resulting truncated sentence.
Best of luck, and happy coding!