I am writing a program (in JavaScript) that should randomly split a string (one word) into groups of letters, each group length (number of characters) being 2-3 or 4 characters. For example, it australiamay return:
aus
tral
ia
or
au
str
alia
I am currently doing this “manually”, with if statements for each line length, for example:
if (word.length == 4){
sections.push(word.substr(0,2));
sections.push(word.substr(2,4));
}
if (word.length == 5){
if (randomBetween(1,2) == 1){
sections.push(word.substr(0,2));
sections.push(word.substr(2,5));
} else {
sections.push(word.substr(0,3));
sections.push(word.substr(3,5));
}
}
etc...
Does anyone have a more algorithmic solution?
source
share