# [LeetCode 75] DAY 6 - Reverse Words in a String
## 題目:
Given an input string `s`, reverse the order of the words.
A word is defined as a sequence of non-space characters. The words in `s` will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that `s` may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
## 解題思考
- `trim()` 刪除頭尾多餘的空白
- `split(' ')` 以<u>一個空白</u>當作分隔符 (回傳一個新陣列)
- `filter()` 篩選掉空白元素
- `reverse()` 反轉陣列
- `join(' ')` 以一個空白合併反轉後的陣列
## 程式碼
```javascript
function reverseWords(s) {
const words = s.trim().split(' ').filter(word => word.length > 0);
let result = words.reverse().join(' ');
return result;
};
```
## 延伸思考
- `filter(Boolean)` 可以篩選掉:`false`, `0` , ``(空字串), `null`, `undefined`, `NaN`
- `split()` 接受正則表達式 ex: `split(/\s+/)`
## 相關資源
- [151. Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/description/)