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
Reverse a String-freecodecamp-js题目
原文地址:https://www.cnblogs.com/ahswch/p/9289728.html