写着玩! 将'abcde'反转为'edcba',用几种方法来实现
第一种
转化为数组然后反转在转回字符串。
const str = 'abcde';
const newStr = str.split('').reverse().join(');
第二种
扩展字符串然后反转然后转为字符串。
const str = 'abcde';
const newStr =[...str].reverse().join();
第三种
使用reduce反转,初始值可填可不填,不填默认数组的第一个值。
const str = 'abcde';
const newStr=Array.from(str).reduce((sum,currentValue)=>{
return currentValue + sum;
})
console.log(newStr)
第四种
循环
const str = 'abcde';
let newStr= ''
for(let i = str.length-1;i>=0 ;i--){
newStr += str[i];
}