Sentence Smash
# Problem Solving
Problem
Write a function that takes an array of words and smashes them together into a sentence and returns the sentence. You can ignore any need to sanitize words or add punctuation, but you should add spaces between each word. Be careful, there shouldn't be a space at the beginning or the end of the sentence!
Example :
Solution
Let me explain how the code works:
- We define a function called smash that takes an array of words as its parameter.
- Inside the function, we use the join method on the words array. The join method concatenates all the elements of an array into a single string, with the specified separator in between. In this case, we use a space ‘ ‘ as the separator.
- The resulting string is stored in the variable sentence.
- Finally, we return the sentence from the function.
- Outside the function, we create an array called words with the given words.
- We call the smash function with the words array as an argument and store the result in the variable result.
- Finally, we log the result to the console, which will display the sentence.
When you run this code, the output will be:
So, the function joins all the words in the array together with spaces and returns the resulting sentence.
JavaScript Playground
© aulianza