[js] 第72天 写一个字符串重复的repeat函数
haizhilin2013 opened this issue · comments
第72天 写一个字符串重复的repeat函数
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'
@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
百度看到的哈哈
const repeat = (str, n)=>str.repeat(n)
const repeat = (str,n)=>str.padEnd(((n+1)*str.length,str)
const repeatStr = (str, num) => {
return Array(num + 1).fill(str).join('')
}
const repeat = (str) => {return typeof(str) === 'string' ?str+str:'type error';}
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_"}));
时间复杂度: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;
}
let a = [...b , ...c].join() b或者c给一个空数组 菜鸡前端 方法有点偏门 不知道对不对
const repeat = function (str, count,sumStr='') { if(typeof str !=='string'){ return 'type error'; } sumStr = sumStr + str; return count !==1 ? repeat(str,--count,sumStr):sumStr; }
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));
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('')
}
}
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("");
}
function repeat(str,n) {
var result = '';
while(n-- > 0){
result += str;
}
return result ;
}
@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
百度看到的哈哈
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)
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))