美文网首页
返回长度为n的随机数字符串的函数

返回长度为n的随机数字符串的函数

作者: 天天要加油 | 来源:发表于2018-08-12 19:50 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>es6</title>
</head>
<body>
<script>
    function Random(n) {
        let max = 1;
        for (let i = 0; i < n; i++) {
            max *= 10;
        }
        return function(){
            let str = "" + parseInt(Math.random() * max);
            let count = n - str.length;
            for (let i = 0 ; i < count; i ++){
                str += '0';
            }
            return str;
        }
    }
    let Random8 = Random(8);
</script>
</body>
</html>

执行结果:


image.png

相关文章

网友评论

      本文标题:返回长度为n的随机数字符串的函数

      本文链接:https://www.haomeiwen.com/subject/uycebftx.html