could you write a JavaScript function that generates all combinations of an inpu
ID: 3796480 • Letter: C
Question
could you write a JavaScript function that generates all combinations of an inputted word. in a way that's different from
function substrings(str1)
{
var array1 = [];
for (var x = 0, y=1; x < str1.length; x++,y++)
{
array1[x]=str1.substring(x, y);
}
var combi = [];
var temp= "";
var slent = Math.pow(2, array1.length);
for (var i = 0; i < slent ; i++)
{
temp= "";
for (var j=0;j<array1.length;j++) {
if ((i & Math.pow(2,j))){
temp += array1[j];
}
}
if (temp !== "")
{
combi.push(temp);
}
}
console.log(combi.join(" "));
}
Explanation / Answer
function combinacion (str)
{
var combo =str.toLowerCase();
var arr=combo.split(");
document.write(arr+' ,');
for (var i=0;i<arr.length;i++)
{
document.write (arr[i]+' , ');
for(var j=0; j<arr.length;j++)
{
if (arr[i]==arr[j])
{
document.write(");
}
else {
document.write (arr[i]+arr[j]+ ' , ') ;
}
}
}
document.write (arr.reverse().join("));
}
var str =combinacion("word:","here"))
========================================================
or
function combString(str)
{
var lenstr=str.length;
var result=[];
var indexCurrent=0;
while (indexCurrent<lenStr)
{
var char =str.charAt(iindexCurrent);
var x;
var arrTemp=[char];
for(x in result)
{
arrTemp.push(""+result[x]+char);
}
result=result.concat(arrTemp);
indexCurrent++;
}
return result;
}
console.log(combString("abc"));//return 7 combine
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.