晴明的博客园 GitHub      CodePen      CodeWars     

[js] 栈 简单使用demo

#

            //进制转换
            function mulBase(num, base) {
                var s = [];
                do {
                    s.push(num % base);
                    console.log(s);
                    num = Math.floor(num /= base);
                    console.warn(num);
                } while (num > 0);
                var converted = '';
                while (s.length > 0) {
                    converted += s.pop();
                    console.error(converted);
                }
                return converted;
            }
            //            console.log(3%10);//3
            //            console.info(mulBase(32, 2));//100000
            //            console.info(mulBase(32, 8));//40
            //            console.info(mulBase(32, 10));
            //            console.info(mulBase(32, 16));//20
            //判断是否是回文
            function isPalindrome(word) {
                var s = [];
                for (var i = 0; i < word.length; ++i) {
                    s.push(word[i]);
                }
                var rword = '';
                while (s.length > 0) {
                    rword += s.pop();
                }
                if (word === rword) {
                    return true;
                } else {
                    return false;
                }
            }
            //            console.info(isPalindrome('hello'));//false
            //            console.info(isPalindrome('lol'));//true
            //阶乘
            function fact(n) {
                var s = [];
                while (n > 1) {
                    s.push(n--);
                }
                var product = 1;
                while (s.length > 0) {
                    product *= s.pop();
                }
                return product;
            }
            //console.info(fact(5)); //120

 

posted @ 2016-03-08 00:22  晴明桑  阅读(144)  评论(0编辑  收藏  举报