Reverse a String(翻轉字元串) 把字元串轉化成數組 藉助數組的reverse方法翻轉數組順序 把數組轉化成字元串 用.split('')將字元串轉換成單個字母組成的數組 用.reverse()把數組反轉 用.join('')把數組元素連接成字元串 .split()方法:http:// ...
Reverse a String(翻轉字元串)
- 題目要求:
- 把字元串轉化成數組
- 藉助數組的reverse方法翻轉數組順序
- 把數組轉化成字元串
- 思路:
- 用.split('')將字元串轉換成單個字母組成的數組
- 用.reverse()把數組反轉
- 用.join('')把數組元素連接成字元串
- 代碼如下:
-
1 function reverseString(str) { 2 // 請把你的代碼寫在這裡 3 var temp = []; 4 temp = str.split(""); 5 temp = temp.reverse(); 6 str = temp.join(""); 7 return str; 8 } 9 10 reverseString("hello");
-
- 相關鏈接
- .split()方法:http://www.runoob.com/jsref/jsref-split.html
- .reverse()方法:http://www.runoob.com/jsref/jsref-reverse.html
- .join()方法:http://www.runoob.com/jsref/jsref-join.html