我正在学习coffeescript并编写了以下函数来反转给定的单词:
reverse = (word) ->
if word.length is 0
return "empty string"
if word.length is 1
return word
left = 0
right = word.length-1
while left < right
swap(word, left, right)
#[word[left], word[right]] = [word[right], word[left]]
left++
right--
return word
swap = (word, left, right) ->
console.log "#{word[left]} #{word[right]}"
temp = word[left]
word[left] = word[right]
word[right] = temp
console.log "#{word[left]} #{word[right]}"
console.log reverse("coffeescript")
但它不起作用。在交换函数本身中,两个索引处的字符不会交换位置。我错过了什么?