zoukankan      html  css  js  c++  java
  • 重复输出字符串

    重复输出字符串

    在 String 对象上定义一个 repeatify 函数。这个函数接受一个整数参数,来明确字符串需要重复几次。这个函数要求字符串重复指定的次数。

    所有的字符串的方法应该添加到String函数构造出的所有字符串上,首先应当判断是否已经有了这个方法,没有则添加。

    最简单的循环方式:

    String.prototype.repeatify = String.prototype.repeatify || function(times){
        var str = '';
        //判断是否正整数
        if(times>0){
            if(typeof times === 'number'&&times%1 === 0){
                for (var i = 0; i < times; i++) {
                    str += this;
                }
                return str;
            }
        }else{
            console.log('参数错误')
            return '';
        }
    };
    console.log('asd'.repeatify(3.1))
    

    ,以下省略判断正整数,

    while循环模式:

    String.prototype.repeatify = String.prototype.repeatify || function(times){
        var str = '';
        while (time>0) {
            str += this;
            time--;
        }
        return str;
    };
    console.log('asd'.repeatify(3))//'asdasdasd'    
    

    拼接字符串可以使用join方法

            var str = [];
            while (times>0) {
                str.push(this);
                times--;
            }
            return str.join('');
    

    递归也可以达到效果:

    String.prototype.repeatify = String.prototype.repeatify || function(times){
        var str = '';
        if(times === 0){
            console.log
            return ''
        }else{
            if(times === 1){
                return this ;
            }else{
                return this + this.repeatify(times-1)
            }
        }
    };
    console.log('asd'.repeatify(3))//'asdasdasd'  
    

    es6中String有repeat方法,接受一个[0,无穷)的整数

    String.prototype.repeatify = String.prototype.repeatify || function(times){
       return times>0?this.repeat(times):''
    };
    console.log('asd'.repeatify(3))//'asdasdasd' 
    

    使用array的fill方法也可以(省略判断)

     return new Array(times).fill(this).join('');  
    

    或者直接join数组

     return new Array(times+1).join(this);
  • 相关阅读:
    cdoj793-A Linear Algebra Problem
    C语言各种数据类型取值范围
    taro 在components文件夹中 新建组件时,组件支持自定义命名,但是不能大写开头
    taro 不支持render中,使用函数多条件渲染
    taro 引用相对路径图片
    taro CSS Modules 的使用
    taro refs引用
    taro 环境判断
    input 禁止 复制 粘贴 剪切 操作
    windows 系统中 use arrow keys怎么操作?
  • 原文地址:https://www.cnblogs.com/mydia/p/6675767.html
Copyright © 2011-2022 走看看