I want to get all neighboring binations of words in string like
string get all binations
and I want to get
get all binations
all binations
get all
all
get
binations
and I write next code
var string = 'get all binations';
var result = getKeywordsList(string);
document.write(result);
function getKeywordsList(text) {
var wordList = text.split(' ');
var keywordsList = [];
while (wordList.length > 0) {
keywordsList = keywordsList.concat(genKeyWords(wordList));
wordList.shift();
}
return keywordsList;
}
function genKeyWords(wordsList) {
var res = [wordsList.join(' ')];
if (wordsList.length > 1) {
return res.concat(genKeyWords(wordsList.slice(0, -1)));
} else {
return res;
}
}
I want to get all neighboring binations of words in string like
string get all binations
and I want to get
get all binations
all binations
get all
all
get
binations
and I write next code
var string = 'get all binations';
var result = getKeywordsList(string);
document.write(result);
function getKeywordsList(text) {
var wordList = text.split(' ');
var keywordsList = [];
while (wordList.length > 0) {
keywordsList = keywordsList.concat(genKeyWords(wordList));
wordList.shift();
}
return keywordsList;
}
function genKeyWords(wordsList) {
var res = [wordsList.join(' ')];
if (wordsList.length > 1) {
return res.concat(genKeyWords(wordsList.slice(0, -1)));
} else {
return res;
}
}
can I improve or simplify this task (get all neighboring binations of words ) p.s. sorry for my English
Share Improve this question edited Oct 27, 2014 at 11:11 dyachenko 1,21214 silver badges29 bronze badges asked Oct 27, 2014 at 8:24 user4184969user4184969 4- 2 If the code works as intended, it's better asked on Code Review. – Jongware Commented Oct 27, 2014 at 8:36
- @Sergey -- just be careful with recursive functions, they are limited depending the browser you use. In fact wouldnt it be better to just send that list server side to handle it with PhP? – kockburn Commented Oct 27, 2014 at 8:38
- @Grimbode thanks I will be careful with it – user4184969 Commented Oct 27, 2014 at 8:41
- @Jongware i didn't know about it – user4184969 Commented Oct 27, 2014 at 8:42
1 Answer
Reset to default 11hello maybe this help you
var string = 'get all binations'
var sArray = string.split(' ');
var n = sArray .length;
for (var i = 0; i < n; i++) {
for (var j = 0; j <= i; j++) {
document.write(sArray .slice(j, n - i + j).join(' ') + ', ');
}
}