js翻转字符串

翻转字符串

    <script>
        // reversevar
        name = "My city is WH";
        var resultStr = name.split('').reverse().join('');
        console.log(resultStr);  // HW si ytic yM // charAtvar 
        name = "My city is WuHan";
        var nameArr = name.split('');
        var resultStr = '';
        for (var i = nameArr.length - 1; i >= 0; i--) {
            resultStr += name.charAt(i);
        }
        console.log(resultStr);  // naHuW si ytic yM
    </script>
posted @ 2021-11-12 17:31  江咏之  阅读(46)  评论(0编辑  收藏  举报