今天小编来给大家实现一下字符串翻转功能:
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<input type="button" value="str" onclick="foo()" />
</body>
<script>
function foo() {
var str = reverse('a,b,c,d,e,f,g');
alert(str);
if (str == 'g,f,e,d,c,b,a')
alert('成功');
else alert('失败');
}
function reverse(str) {
// 在此处加入代码,完成字符串翻转功能
// **********Found**********
var spl = str.split(',');
var newstr = "";
// **********Found**********
for (i = spl.length - 1; i >= 0; i--) {
// **********Found**********
newstr += spl[i] + ',';
}
newstr = newstr.substring(0, newstr.length - 1)
return newstr;
}
</script>
</html>
功能如下: