实战翻转字符串算法
你可以先把字符串转化成数组,再借助数组的reverse方法翻转数组顺序,然后把数组转化成字符串。
提示:你的结果必须得是一个字符串
function reverseString(str) {
//字符串翻转
var arr = str.split('');//使用split()方法转换为数组 ["h","e","l","l","o"]
arr.reverse();//使用reverse()方法翻转数组 ["o","l","l","e","h"]
str = arr.join('');//使用join()方法合并数组为字符串 "olleh"
}
reverseString("hello");
reverseString("hello")
应该返回一个字符串
reverseString("hello")
应该返回 "olleh"
.
reverseString("Howdy")
应该返回 "ydwoH"
.
reverseString("Greetings from Earth")
应该返回 "htraE morf sgniteerG"
.