0
3. Write a JavaScript function that generates all combinations of a string.
11 Respuestas
+ 3
this question is really hard.
i did it once using recursion.
and considering all letters in string are different.
+ 2
function combinator (s) {
list_of_strings = new Array();
for(i=0;i<s.length;i++) {
for(j=i+1;j<s.length+1;j++) {
list_of_strings.push(s.slice(i, j));
}
}
return list_of_strings;
}
document.write(combinator("dog"));
+ 1
please answer
+ 1
to java script
+ 1
You'll have to change the last line to make it suitable
+ 1
thank you
+ 1
my pleasure
+ 1
Write a JavaScript function that returns a .4 passed string with letters in alphabetical 'order. Example string : 'webmaster 'Expected Output : 'abeemrstw Assume punctuation and numbers symbols .are not included in the passed string
+ 1
3. Write a JavaScript function that generates all combinations of a string.
function combinator (s) {
list_of_strings = new Array();
for(i=0;i<s.length;i++) {
for(j=i+1;j<s.length+1;j++) {
list_of_strings.push(s.slice(i, j));
}
}
return list_of_strings;
}
document.write(combinator("dog"));
0
Hello guys! I've got a new simple solution using built-in functions
0
function combine(str){
const result = [];
for(let i = 1; i < Math.pow(2, str.length) - 1; i++)
result.push([...str].filter((_, pos) => (i >> pos) & 1).join(""));
return result;
}
console.log(combine("dog"));