0

# Stop gninnipS My sdroW!

### 原始題目

Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (like the name of this kata).
Strings passed in will consist of only letters and spaces.
Spaces will be included only when more than one word is present.

Example:

``````spinWords("Hey fellow warriors") => "Hey wollef sroirraw"
spinWords("This is a test") => "This is a test"
spinWords("This is another test") => "This is rehtona test"
``````

### 自解:

``````export class Kata {
static spinWords(words: string) {
let arr = words.split(" ");
arr.forEach((e,index) => {
if(e.length >= 5 ){
let reverse = e.split('').reverse().join('');
arr.splice(index,1,reverse)
}
})
return arr.join(" ")
}
}
``````

1. 先將字串全部拆成一個個字，並存於array中。
2. 將Array內的每個字逐一檢查是否長度有大於等於5
3. 若長度大於等於5的，找到該字在Array中的index,並透過splice方法將該值換成reverse過後的值。
(splice的使用請見下方參考資料)

### 他解:

`author: joewoodhouse`

``````export class Kata {
static spinWords(words: string) {
return words
.split(' ') // Split words up
.map((w: string) => w.length >= 5 ? w.split('').reverse().join('') : w)
.join(' '); // Put them back together
}
}
``````

https://catforcode.com/ternary-operators/

### 此次題目使用到的方法和參考網址:

array.splice() 可以藉由刪除既有元素並／或加入新元素來改變一個陣列的內容。
https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/splice