Skip to content

Instantly share code, notes, and snippets.

@Kreijstal
Created October 17, 2014 22:56
Show Gist options
  • Save Kreijstal/6169fef30298adbc284e to your computer and use it in GitHub Desktop.
Save Kreijstal/6169fef30298adbc284e to your computer and use it in GitHub Desktop.
function agrupateBy3CharacterGroups(words){
var wordsAnswer=[],wordGroups=[],word;
for(var i=0;i<words.length;i++){
word=Word3Chars(words[i]);
for(var _i=0;;_i++){
if(!wordGroups[_i]){wordGroups[_i]=word;wordsAnswer[_i]=words[i];break}else{
if(wordsAnswer[_i].length<120&&!hasSharedChildren(wordGroups[_i],word)){
wordsAnswer[_i]+=" "+words[i];wordGroups[_i]=wordGroups[_i].concat(word);break
}
}
}
}
return wordsAnswer
}
function Word3Chars(word){
var a=[]
for(var i=0;i<word.length-2;i++){
//console.log(word.substr(i+2,1))
if(word.substr(i+2,1)===" ")i+=3;
if(word.substr(i+1,1)===" ")i+=2;
a.push(word.substr(i,3).trim())
}
return a;
}
function hasSharedChildren(array1,array2){
for(var i=0;i<array1.length;i++){
if(array2.indexOf(array1[i])!==-1)return true
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment