function reverse(str){
/*
steps:
1. check type
2. check the length of the string
3. if input exist
*/
if(typeof str !== 'string' || str.length <= 1 || !str){
return 'Invalid input!';
}
const backwords = [];
/*
method 01:
for(let i = str.length-1; i >= 0; i--){
backwords.push(str[i]);
}
*/
let times = str.length;
// methods 02:
while(times){
backwords.push(str[times-1])
times--;
}
return backwords.join('');
}
console.log(reverse('hello world')); //dlrow olleh
reverse string
最新推荐文章于 2024-08-06 10:39:23 发布