# Comments

19 days ago

const solve = (strArg, k) => { return Array.from(new Array(k), () => strArg); };

2 months ago

Python solution with simple `while` loop and `append()` method

``````class Solution:
def solve(self, str_arg, k):
result = []
while(len(result) < k):
result.append(str_arg)
return result
``````

6 months ago

`const solve = (strArg, k) => { let result = []; while(k != 0){ result.push(strArg); k-- } return result; };`

6 months ago

const solve = (strArg, k) => { return Array(k).fill(strArg) };

8 months ago

const solve = (strArg, k) => {

``````let newArr = [];

for(let i = 0; i < k; i++){
newArr.push(strArg)
}

return newArr;
``````

};

a year ago

Is there anybody who can explain why recursion fails in a case of any other symbol like dash ? So when I run the code in my editor it works out with "hi" any times and breaks with a sentance where a comma is used. The question is.... why does the code break provided I split a string by a dash (not comma) ? Strange behaviour....

``````const solve = (str, k) => (k >= 1)
? (str + (k > 1 ? ',' : '') + solve(str, k - 1)).split(',')
: ''
``````

a year ago

``````const solve = (strArg, k) => {
return new Array(k).fill(strArg)
};

``````

a year ago

an old school approach :

``````const solve = (strArg, k) => {
const result = [];
for(let i = 0; i < k; i++ ){
result.push(strArg);
}
return result;
};

``````

a year ago

const solve = (strArg, k) => { const arr = [];

while(arr.length !== k) { arr.push(strArg); } return arr; };

2 years ago

const solve = (strArg, k) => { let strArg1 = strArg.length; return strArg.repeat(k).match(new RegExp('.{1,' + strArg1 + '}', 'g')); };

2 years ago

const solve = (strArg, k) => { let arr=new Array(k); return arr.fill(strArg) };

2 years ago

You can use a little bit of ES2015 magic to make this really easy, specifically the `fill` method.

``````const solve = (strArg, k) => {
return Array(k).fill(strArg);
};
``````

`Array(k)` creates a new array of the given length, then the `fill` statement puts in `strArg` at each index.