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.
- 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.
Here are a few examples to help you understand the problem better:
truncateSentence("Hello how are you Contestant", 4);
Output: "Hello how are you"
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".
truncateSentence("What is the solution to this problem", 4);
Output: "What is the solution"
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".
truncateSentence("chopper is not a tanuki", 5);
Output: "chopper is not a tanuki"
- Ensure you read the input string carefully.
- Split the input string by space and extract the first
- Join the extracted words to form a truncated sentence.
- Return the resulting truncated sentence.
Best of luck, and happy coding!