我看過例子,但我仍然不明白所有這些。我知道 substring() 肯定會被使用,但如果可能的話,我需要逐步分解。非常感謝 :)
uj5u.com熱心網友回復:
首先,您可以使用方法拆分字串String.split(),然后您可以通過推入新陣列來創建與元素的組合。
作業演示:
// 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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/433678.html
標籤:javascript 数组 细绳
