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 :)
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 :
// Input string
let str = "dog";
// An empty array which will store the combinations.
let result = [];
/**
* getCombination() method is used to get all the combinations of all the array elements passed as a parameter.
*/
function getCombination(inputArr) {
// variable which will store the combination string.
let temp = '';
// Iterating input array to get elements one by one.
inputArr.forEach((elem, index) => {
// Pushing the element into result array.
result.push(temp + elem);
// To make the combination updating temp variable with the elem.
temp += elem + "";
});
}
// Once combination done for one iteration, removing the first element from an array and then again calling getCombination() method to get the combination for next set of elements.
str.split('').forEach((elem, index) => {
getCombination([...str.slice(index)]);
});
// Expected result
console.log(result);