Repeat a string repeat a string

Repeat a string repeat a string
0

#1

Hi, THIS WORKS.
But maybe you did it on another way, so share yours here, or give me your advices.
Thanks guys!!!

function repeatStringNumTimes(str, num) {
// repeat after me
var result=[""];
if (num>0){
for (var i=; i<(num);i++){
result.push(str);
}
result.shift();
return result.join("");
}
else {
return “”;
}

}

repeatStringNumTimes(“abc”, 5);


#2

What you did is perfectly fine and I think I wrote something similar the first time around. The only problem is that it is rather long. It’s not an issue now but if you ever need to deal with something more complex, read up on some of the string functions. It might save you a lot of time and effort. This is a one line solution to the same problem:


function repeatStringNumTimes(str, num) {
  return num > 0 ? str.repeat(num) : '';
}


#3

@mkarabashev
Elegant and brief, nice. I would be careful using it, though. string.prototype.repeat is part of ecmascript 6 and not supported on all common browsers (safari mac/ios, android, see http://kangax.github.io/compat-table/es6/).

I use this ugly thing:

function repeatStringNumTimes(str, num) {
    return new Array(num+1).join(str);
}

#4

This is great and I learned something new ! For this particular challenge i’d had this conditional check for num value :

function repeatStringNumTimes(str, num) {
    return num > 0 ? new Array(num+1).join(str) : '';
}

Because it’s asked to return an empty string when num is < 0


#5

whaou folk…
you all write very short codes… :heart_eyes:
Where (how) could I learn such skills???


#6

Keep on coding and look up stuff you don’t know yet. For instance, I borrowed my implementation from stack overflow :-).


#7

Nice … struggled understanding what new Array(num+1) did …tried it myself to see what it did but still couldn’t figure out why it works … so googled a bit and found “The new keyword only complicates the code. It can also produce some unexpected results:”.var points = new Array(40); // Creates an array with 40 undefined elements !!! .
so in my console i type and enter new Array(40) and get back [ undefined * 40 ] so now i understand the result is gotten by using a glitch … lol nice how did you come up with the join() part. Anyway really like this answer,


#9

This is my solution, I feel its rather long.

Any suggestions on shortening it or using different methods is appreciated.


function repeatStringNumTimes(str, num) {
var stringRepeated = [];

for (var i = 0; i < num; i++) {
stringRepeated.push(str);
}

var combineArray = stringRepeated.join(’’);

return combineArray;
}

repeatStringNumTimes(“abc”, 3);


#10

why doesnt this work? var k=[];
function repeatStringNumTimes(str, num) {
// repeat after me
for(i=0;i<num;i++){

k=k+str;

}
return k;

}

repeatStringNumTimes(“abc”, 3);


#11

function repeatStringNumTimes(str, num) {
// repeat after me
var conc = [];
if (num <= 0) {
return “”;
}else {
for (var i = 0; i < num; i++) {
conc += str;
}
}
return conc;
}

repeatStringNumTimes(“abc”, 3);


#12

Wait - there’s a repeat method? Since when


#14

IS ES6 now ‘accepted’ generally or still in the acceptance process?


#16

not sure if this will return null if num is 0.


#17

here my solution with for loop:

function repeatStringNumTimes(str, num) {
var repeatedStr="";
for(i=0;i<num;i++){
repeatedStr +=str;
}
return repeatedStr;
}

repeatStringNumTimes(“abc”, -3);


#18

@fotonist, that’s the same code I’ve done actually but mine isn’t passing a single check. Maybe I’m missing something?

var finalString = "";
function repeatStringNumTimes(str, num) {
  for (i = 0; i < num; i++) {
    finalString = finalString.concat(str);
  }
  return finalString;
}

repeatStringNumTimes("abc", 3);

#19

Solutions with global variables are generally not accepted in the challenges. Move the finalString variable inside the function.


#20

Thanks, that makes sense.


#21

Hey, if you have few minutes to respond me, could you help me why this code didnt work :
Thanks!


function repeatStringNumTimes(str, num) {

Array.prototype.map.call(str, function(x) {
return x*num;
}).join(’’);

}

repeatStringNumTimes(“abc”, 3);


#22

These challenges give me a headache sometime and feel of discouragmnet, even the simple ones. This is only my second challenge I solved on my own.

function repeatStringNumTimes(str, num) {
  if (num > 0) {
    var multiplyStr = str.repeat(num);
    return multiplyStr;
  } else {
    return "";
  }
}

repeatStringNumTimes("abc", 3);

#23

hello acid4207,
Whenever you find convenient can you explain your logic behind this step
for (var i = 0; i < num; i++) {
conc += str;