Have function to show subsets of a string - now trying to adapt code to
handle arrays
Some working code is on the bottom. But my poorly adapted code on the top
goes into an infinite recursion loop. What is it I do not know about
arrays?
function recSubsets(soFar, rest)
{
if (rest===[]) console.log(soFar);
else
{
recSubsets(soFar.push(rest[0])), rest.slice(1));
recSubsets(soFar, rest.slice(1));
}
}
function listSubsets(s)
{
recSubsets([],s);
}
listSubsets([4,9,3,77])
below is the working version for strings
function recSubsets(soFar, rest)
{
if (rest==="") console.log(soFar);
else
{
recSubsets(soFar+rest[0], rest.substring(1));
recSubsets(soFar, rest.substring(1));
}
}
function listSubsets(s)
{
recSubsets("",s);
}
listSubsets("cat")
gives me:
cat ca ct c at a t
No comments:
Post a Comment