Home > Blockchain >  How to write a javascript program that returns the subset of a string as an array?
How to write a javascript program that returns the subset of a string as an array?

Time:02-28

I have seen examples but I still dont understand all of it. I know substring() is definately used but I need a step by step breakdown if possible. Thank you very much :)

CodePudding user response:

First you can split the string using String.split() method and then you can create combinations with the elements by pushing in new array.

Working Demo :

let str = "dog";
let result = [];

function getCombination(inputArr) {
  let temp = '';
  inputArr.forEach((elem, index) => {
    result.push(temp   elem);
    temp  = elem   "";
  });
}

str.split('').forEach((elem, index) => {
    getCombination([...str.slice(index)]);
});


console.log(result);

  • Related