Retired

String chunks (retired)

457 of 1,020keune

Description:

You should write a function that takes a string and a positive integer n, splits the string into parts of length n and returns them in an array. It is ok for the last element to have less than n characters.

If n is not a number or not a valid size (> 0) (or is absent), you should return an empty array.

If n is greater than the length of the string, you should return an array with the only element being the same string.

Examples:

stringChunk('codewars', 2) // ['co', 'de', 'wa', 'rs']
stringChunk('thiskataeasy', 4) // ['this', 'kata', 'easy']
stringChunk('hello world', 3) // ['hel', 'lo ', 'wor', 'ld']
stringChunk('sunny day', 0) // []
stringChunk('codewars', 2) # ['co', 'de', 'wa', 'rs']
stringChunk('thiskataeasy', 4) # ['this', 'kata', 'easy']
stringChunk('hello world', 3) # ['hel', 'lo ', 'wor', 'ld']
stringChunk('sunny day', 0) # []
string_chunk('codewars', 2) # ['co', 'de', 'wa', 'rs']
string_chunk('thiskataeasy', 4) # ['this', 'kata', 'easy']
string_chunk('hello world', 3) # ['hel', 'lo ', 'wor', 'ld']
string_chunk('sunny day', 0) # []
Strings
Fundamentals

More By Author:

Check out these other kata created by keune

Stats:

CreatedJul 26, 2015
Warriors Trained1900
Total Skips174
Total Code Submissions11536
Total Times Completed1020
JavaScript Completions578
CoffeeScript Completions14
Python Completions457
Total Stars32
% of votes with a positive feedback rating82% of 223
Total "Very Satisfied" Votes165
Total "Somewhat Satisfied" Votes37
Total "Not Satisfied" Votes21
Ad
Contributors
  • keune Avatar
  • joh_pot Avatar
  • CrazyMerlyn Avatar
  • bojidar-bg Avatar
  • siebenschlaefer Avatar
  • user1430804 Avatar
  • Wei-Ting Yang Avatar
  • saudiGuy Avatar
Ad