Skip to content

[js] 第72天 写一个字符串重复的repeat函数 #511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
haizhilin2013 opened this issue Jun 26, 2019 · 19 comments
Open

[js] 第72天 写一个字符串重复的repeat函数 #511

haizhilin2013 opened this issue Jun 26, 2019 · 19 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第72天 写一个字符串重复的repeat函数

@haizhilin2013 haizhilin2013 added the js JavaScript label Jun 26, 2019
@AnsonZnl
Copy link
Contributor

AnsonZnl commented Jun 27, 2019

var str='abcd';
function repeat(str,n){
    var type = typeof(str) === 'string';
    var result='';
    if(!type){
        return 'Type Error';
    }
    for(var i=0;i<n;i++){
        result += str;
    }
    return result;
}
repeat(str,2);//'abcdabcd'

@haizhilin2013
Copy link
Collaborator Author

@AnsonZnl 不错,还有别的方法吗?

@AnsonZnl
Copy link
Contributor

@AnsonZnl 不错,还有别的方法吗?

var str='abcd';
function repeat(str,n){
    if((typeof str) === 'string'){
       return (new Array(n+1)).join(str)
    }
    return 'Type Error'
 }
 repeat(str,3)//abcdabcdabcd

百度看到的哈哈

@haizhilin2013
Copy link
Collaborator Author

@AnsonZnl 也不错,还有别的方法吗?

@thisisandy
Copy link

const repeat = (str, n)=>str.repeat(n)

@thisisandy
Copy link

const repeat = (str,n)=>str.padEnd(((n+1)*str.length,str)

@maoqxxmm
Copy link

maoqxxmm commented Jun 28, 2019

const repeatStr = (str, num) => {
  return Array(num + 1).fill(str).join('')
}

@leehf
Copy link

leehf commented Jul 5, 2019

const repeat = (str) => {return typeof(str) === 'string' ?str+str:'type error';}

@Konata9
Copy link

Konata9 commented Jul 5, 2019

const repeatStr = ({str = "", repeat = 1}) => {
  if (!str || repeat === 0) {
    return str;
  }

  return (str += repeatStr({str: str, repeat: (repeat -= 1)}));
};

console.log(repeatStr({str: "abc", repeat: 3}));
console.log(repeatStr({str: "abc_e", repeat: 2}));
console.log(repeatStr({str: "abc||", repeat: 4}));
console.log(repeatStr({str: "a b c_", repeat: 2}));
console.log(repeatStr({str: "_abc_", repeat: 3}));
console.log(repeatStr({str: "_abc_"}));

@ahonn
Copy link

ahonn commented Jul 12, 2019

时间复杂度:O(logN)

const repeat = (str, count) => {
  if (count === 0) {
    return '';
  }
  if (count === 1) {
    return str;
  }

  let result = repeat(str + str, count >> 1)
  if (count & 1) {
    result += str;
  }
  return result;
}

@Alonggg
Copy link

Alonggg commented Jul 16, 2019

let a = [...b , ...c].join() b或者c给一个空数组 菜鸡前端 方法有点偏门 不知道对不对

@wyx2014
Copy link

wyx2014 commented Jul 25, 2019

const repeat = function (str, count,sumStr='') {
    if(typeof str !=='string'){
        return 'type error';
    }
    sumStr = sumStr + str;
    return count !==1 ? repeat(str,--count,sumStr):sumStr;
}

@Vi-jay
Copy link

Vi-jay commented Jul 31, 2019

String.prototype.repeat = function (count) {
  let originStr = String(this);
  let thisStr = this;
  for (let i = 1; i < count; i++) {
    thisStr += originStr;
  }
  return thisStr
};
console.log("asd".repeat(2));

@woodPeckerAnos
Copy link

woodPeckerAnos commented Aug 12, 2019

ES6本身提供了String实例的repeat方法,如果还要手写,一般是考虑兼容性问题,得用旧的语法

function repeat (str, times) {
    if (typeof String.prototype.repeat === 'function') {
      return str.repeat(times)
    } else {
      var arr = new Array(times)
      for (var i = 0; i < times; i++) {
        arr[i] = str
      }
      return arr.join('')
    }
  }

@seho-dev
Copy link

var str = "123";
var number = 4;

console.log(test4());

function test1() {
return str.repeat(number);
}

function test2() {
return new Array(number + 1).join(str);
}

function test3() {
let result = "";
for (let i = 0; i < number; i++) {
result += str;
}
return result;
}

function test4() {
let arr = new Array(number);
for (let key in arr) {
arr[key] = str;
}
return arr.join("");
}

@zhangkuibao
Copy link

function repeat(str,n) {
var result = '';
while(n-- > 0){
result += str;
}
return result ;
}

@smile-2008
Copy link

@AnsonZnl 不错,还有别的方法吗?

var str='abcd';
function repeat(str,n){
    if((typeof str) === 'string'){
       return (new Array(n+1)).join(str)
    }
    return 'Type Error'
 }
 repeat(str,3)//abcdabcdabcd

百度看到的哈哈

@1684838553
Copy link

1684838553 commented Dec 9, 2021

String.prototype.myRepeat = function(){
    const num = arguments[0]
    const str = this.toString()
    if(typeof num !== 'number'){
        throw new Error('参数要为number类型')
    }
    let result = ''
    if(num === 0){
        result = ''
    } else if( num > 0 && num !== Infinity  ){
        for(let i = 0;i < num ; i++){
            result += str
        }
    } else {
        throw new Error('请输入正确的参数')
    }
    return result
}
const str = 'repeat'
let newStr = str.myRepeat(5)
console.log(newStr)

@xiaoqiangz
Copy link

function repeat(str, num) {
if (typeof num !="number") {
throw new Error('必须为number类型')
}
let result = ''
if (num > 0 && num < Infinity) {
// for(let i=0; i < num; i++) {
// result += str
// }
result = new Array(num).join(str)
}
return result
}

  • console.log(repeat('res26', 5))
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests