Comments

Anurag Arwalkar19 days ago

function solve(strArg, k) { const result = [];

for (let i = 0; i < strArg.length; i += k) { const newItem = strArg.slice(i, i + k); result.push(newItem); }

return result; }

Muhammad Aamira month ago

Solution without splice and slice.

const isLastItem = (index, length) => {
  return index === length - 1;
}

const solve =  (strArg, k) => {
  let chunks = [];
  let chunkValue = "";
  
  for(let index =0; index < strArg.length; index++) {
    chunkValue += strArg[index];
    let cond1 = (index + 1) % k === 0;
    let cond2 = isLastItem(index, strArg.length);
   
    if(cond1 || cond2) {
      chunks.push(chunkValue);
      chunkValue = "";
    }
  }
 
  return chunks;
};

Muhammad Aamira month ago

Solution without splice and slice ` const isLastItem = (index, length) => { return index === length - 1; }

const solve = (strArg, k) => { let chunks = []; let chunkValue = "";

for(let index =0; index < strArg.length; index++) { chunkValue += strArg[index]; let cond1 = (index + 1) % k === 0; let cond2 = isLastItem(index, strArg.length);

if(cond1 || cond2) {
  chunks.push(chunkValue);
  chunkValue = "";
}

}

return chunks; };

`

SDD Product5 months ago

const solve = (strArg, k) => {
  let chunks = [];
  let strArr = strArg.split("");
  for (let i = 0; i < strArr.length; i += k) {
    chunks.push(strArr.slice(i, i + k).join(""));
  }
  return chunks;
};

Bhargav7 months ago

const solve = (str, k) => {
  var res = []
  
  while(str != ''){
    res.push(str.slice(0,k))
    str = str.slice(k)
  }
  return res
};

Mac Villegas8 months ago

const solve = (strArg, k) => { let str=""; let counter = 0; let newArr=[] for(let a = 0; a < strArg.length; a++) { if(counter < k){ str+=strArg[a] counter++; } else { newArr.push(str) counter = 1; str ="" + strArg[a] }
} newArr.push(str) return newArr; };

Fuad Shukurov8 months ago

const solve = (strArg, k) => { var newArr = [] for (i = 0; i < strArg.length; i = i + k) { newArr.push(strArg.slice(i, i + k)) } return newArr }

reboot Practical8 months ago

const solve = (strArg, k) => { let counter = Math.floor(strArg.length / k) let start = 0; let finish = k; const res = []; while (counter !== 0){ res.push(strArg.substring(start, finish)); start = finish; finish = finish + k; counter -= 1; } start < strArg.length && res.push(strArg.substring(start, finish)); return res; };

reboot Practical8 months ago

Ooops, I mistype const solve = (strArg, k) => { let counter = Math.floor(strArg.length / k) let start = 0; let finish = k; const res = []; while (counter !== 0){ res.push(strArg.substring(start, finish)); start = finish; finish += k; counter -= 1; } start < strArg.length && res.push(strArg.substring(start, finish)); return res; };

Alexeya year ago

const solve = (strArg, k) => { const times = Math.ceil(strArg.length / k) const newArr = [] const strArgSplit = strArg.split("") for (let time = 0; time < times; time++) { newArr.push(strArgSplit.splice(0, k)) } return newArr; };

Internal Bacon Polariseda year ago

It's a little confusing to write in one line by python sample code

class Solution:
  def solve(self, str_arg, k):
    return [str_arg[idx:idx+k] for idx in range(0, len(str_arg), k)]

Sarun Chinskula year ago

Can I use this style of code for screen interview or white board interview ?

const solve = (strArg, k) => {

let reg = new RegExp( '(.{'+k+'}|.{1,'+k+'})(.*)?' , 'g' );

let stack = []; while ( strArg.length > 0 ) { let cut = strArg.replace( reg , '$1' );
strArg = strArg.replace( reg , '$2' ); stack.push( cut ); }

return stack; };

Ameliorated Tugrika year ago

Well, I have no idea why it doesn't accept my code, I've already tested in PyCharm - everthing works!

const solve = (str, k) => {
  const arr = [];
  let count = 1;
  let temp = '';
  
  for (s of str) {
    temp += s;
    if (count % k === 0 || count === str.length) {
      arr.push(temp);
      temp = '';
    }
    count++;
  }
  return arr;
};

Code Boia year ago

const solve = (strArg, k) => {
  const chunks = []
  const pieces = strArg.split('')
  while(pieces.length) {
    chunks.push(pieces.splice(0, k).join(''))
  }
  return chunks;
};``

Home 1080p invoicea year ago

const solve = (strArg, k) => { let length = strArg.length; let finalArr = [];

let i=0

while(i+k<length){ finalArr.push(strArg.substring(i,i+k)) i = i+k }

finalArr.push(strArg.substring(i, length))

return finalArr

};

Tim Junga year ago

const solve = (strArg, k) => { const results = []; let count = 0; while (count < strArg.length) { results.push(strArg.slice(count, count + k)) count = count + k; } return results; };

Unbranded online Bordersa year ago

const solve = (strArg, k) => {
  
  let result = [];
  const len = strArg.length + k;
  let begin = 0;
  let end = k;
  
  while(end < len)
  {
    result.push(strArg.slice(begin, end));
    begin = begin + k;
    end = end + k;
  }
  
  return result;
};

Mark Standena year ago

This was a good one, thanks Stephen! Here's my effort:

const solve = (strArg, k) => {
  const slicedArray = []
  const highestMultiple = strArg.length - (strArg.length % k);
  let prevIndex = 0;
  for (let i = k; i <= highestMultiple; i = i + k) {
    let currentSlice = strArg.slice(prevIndex, i);
    slicedArray.push(currentSlice);
    prevIndex = i;
  }
  if (strArg.length % k != 0) {
    const remainder = strArg.slice(highestMultiple, strArg.length)
    slicedArray.push(remainder)
  }
  return slicedArray
};```

Mohamed El Ghannaya year ago


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

govardhana year ago

Using Recursion: const solve = (strArg, k, arr=[]) => { if(!strArg) return arr; arr.push(strArg.slice(0,k)); return solve(strArg.substr(k),k,arr); };

Philip Costachea year ago

`const solve = (strArg, k) => { const arr = []; let index = 0;

while(index < strArg.length) { arr.push(strArg.substring(index, index + k)); index += k; } return arr; };`

Practical Streamlined2 years ago

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

while (strArg.length > 0) { result.push(strArg.slice(0,k)); strArg = strArg.substring(k); }

return result; };

Tony Duane Smith2 years ago

Tried this in a for loop but something kept going wrong so in the end used a while loop

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

Omar Gerardo Rodriguez Aramburo2 years ago

One liner.

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

Omar Gerardo Rodriguez Aramburo2 years ago

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

Kelly Stratton2 years ago

Oh man, I thought I was so clever with this! Nice one Stephen!

// String -> Arr[String]

const solve = (strArg, k) => 
  Array.from({length: Math.ceil(strArg.length/k)}, (_, i) => 
    strArg.substring((i*k), k*(i+1))
  );

Mouse Tools2 years ago

const solve = (strArg, k) => { let arr=[] while(strArg.length>0){ arr.push(strArg.slice(0,k)) strArg = strArg.substr(k,strArg.length) } return arr; };

Savings Account Ergonomic2 years ago

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

Stephen Grider2 years ago

Gettin clever with slice:

const solve = (strArg, k) => {
  const chars = strArg.split('');
  const groupedChars = [];

  for (let key in Array(Math.ceil(chars.length / k)).fill('')) {
    key = parseInt(key);
    
    groupedChars.push(chars.slice(key * k, key * k + k).join(''));
  }
  
  return groupedChars;
};