He visto ejemplos, pero todavía no entiendo todo. Sé que substring() definitivamente se usa, pero necesito un desglose paso a paso si es posible. Muchísimas gracias :)
Primero puede dividir la cadena usando el método String.split() y luego puede crear combinaciones con los elementos presionando una nueva matriz.
Demostración de trabajo:
// 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);