function reverseWord( str ){ // 首先把字符串拆分成数组 let strArray = str.split(' '); console.log('数组--'+strArray); let i = 0, j = strArray.length - 1; while(i < j){ let temp = strArray[i]; strArray[i] = strArray[j]; strArray[j] = temp; i++; j--; } console.log("交换后的数组"+strArray) return strArray.join(' '); } let array = reverseWord("It's a Dog!"); document.write("结果"+array);