Comments

Anurag Arwalkar19 days ago

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

Oscar Daniel González2 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

Ali Sequeira6 months ago

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

Mobility Grenada6 months ago

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

Mac Villegas8 months ago

const solve = (strArg, k) => {

let newArr = [];

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

return newArr;

};

Ameliorated Tugrika 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(',')
  : ''

Samuel Egwurubea year ago

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

Mohamed El Ghannaya year ago

an old school approach :

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

Philip Costachea year ago

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

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

zunair jarral2 years ago

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

Mouse Tools2 years ago

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

Stephen Grider2 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.