JavaScript翻转字符串,把hello world. 变成 dlrow. olleh
来源:互联网 发布:多益网络公司地址 编辑:程序博客网 时间:2024/06/05 22:49
//使用数组翻转函数
function reverseString(str) {
var array = str.split('');//转换成字符串数组
array = array.reverse();
str = array.join('');
return str;
}
//简写
function reverseString1(str) {
return str.split('').reverse().join('');
}
console.log(reverseString("hello world"));//dlrowolleh
console.log(reverseString1("hello world"));//dlrowolleh
//使用for循环
function reverseString2(str) {
var newStr = "";
for (var i = str.length -1; i >= 0; i--) {
newStr += str[i];
}
return newStr;
}
console.log(reverseString2("helloworld"));//dlrowolleh
//使用递归
function reverseString3(str) {
if (str === "") {
return "";
} else {
return reverseString3(str.substr(1)) + str.charAt(0);
}
}
console.log(reverseString3("helloworld"));//dlrowolleh
//简写
function reverseString4(str) {
return (str === '') ? '' : reverseString(str.substr(1)) + str.charAt(0);
}
console.log(reverseString4("helloworld"));//dlrowolleh
阅读全文
0 0
- JavaScript翻转字符串,把hello world. 变成 dlrow. olleh
- 字符串翻转集合, case1, hello world->world hello; case2, hello world->olleh dlrow
- 字符串翻转集合, case1, hello world->world hello; case2, hello world->olleh dlrow
- hello world 输出 olleh dlrow
- 写一段代码将Hello World转换成dlroW olleH。
- dlrow !olleh
- javascript hello world
- javascript hello world
- JavaScript显示hello world
- php拓展变成—— hello world
- 对输入的字符串进行反转并显示结果,例如输入字符串为hello,则程序应输出olleh。
- JavaScript——Hello World
- 终于把Hello World!跑起来了
- 将字符串【"Hello world" 】 倒序结果为【"world Hello " 】
- Hello World!【Hello World】
- 如何把字符串的字典,变成字典
- join()方法,可以把数组变成字符串
- JS把字符串变成变量名
- C/C++ 拼图游戏 代码简单 带有graphics图形库
- JSON学习记录
- 安装APK文件
- RocketMQ原理解析-consumer 6.pull消息消费
- 算法---比较排序算法
- JavaScript翻转字符串,把hello world. 变成 dlrow. olleh
- POJ-1724 ROADS
- NSInvocation 详解
- 一些需要解决的东西
- LinuxC学习日常——自习(1)
- linux 清除左边一列的”
- 创建不会被系统扫描文件夹
- Linux防火墙iptables(一)入门介绍(扫盲)
- codevs1132 瑞士轮 归并排序