-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c994b98
commit 3ac0a79
Showing
3 changed files
with
62 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,66 @@ | ||
function generatePermutations(inputString) { | ||
const uniquePermutations = []; | ||
|
||
function generateUniquePermutations(arr, currentIndex) { | ||
function generateUniquePermutations(arr, currentIndex, length) { | ||
if (currentIndex === arr.length - 1) { | ||
uniquePermutations.push(arr.join("")); | ||
const newPermutation = arr.slice(0, length).join(""); | ||
if (!uniquePermutations.includes(newPermutation)) { | ||
uniquePermutations.push(newPermutation); | ||
} | ||
} else { | ||
for (let i = currentIndex; i < arr.length; i++) { | ||
[arr[currentIndex], arr[i]] = [arr[i], arr[currentIndex]]; | ||
generateUniquePermutations([...arr], currentIndex + 1); | ||
generateUniquePermutations([...arr], currentIndex + 1, length); | ||
} | ||
} | ||
} | ||
for (let length = 2; length <= inputString.length; length++) { | ||
generateUniquePermutations(inputString.split(""), 0, length); | ||
} | ||
|
||
generateUniquePermutations(inputString.split(""), 0); | ||
return uniquePermutations; | ||
} | ||
|
||
|
||
|
||
function descrambleWords() { | ||
const input = document.getElementById('scrambledInput').value; | ||
const combinations = generatePermutations(input); | ||
displayResults(combinations); | ||
const categorizedWords = categorizeByLength(combinations); | ||
displayResults(categorizedWords); | ||
} | ||
|
||
function categorizeByLength(combinations) { | ||
const categorized = {}; | ||
combinations.forEach(word => { | ||
const length = word.length; | ||
if (!categorized[length]) { | ||
categorized[length] = []; | ||
} | ||
categorized[length].push(word); | ||
}); | ||
return categorized; | ||
} | ||
|
||
function displayResults(combinations) { | ||
function displayResults(categorizedWords) { | ||
const resultsContainer = document.getElementById('resultsContainer'); | ||
resultsContainer.innerHTML = ''; | ||
|
||
const wordList = document.createElement('ul'); | ||
combinations.forEach(word => { | ||
const listItem = document.createElement('li'); | ||
listItem.textContent = word; | ||
wordList.appendChild(listItem); | ||
}); | ||
resultsContainer.appendChild(wordList); | ||
for (const [length, words] of Object.entries(categorizedWords)) { | ||
const section = document.createElement('div'); | ||
section.className = 'result-section'; | ||
|
||
const header = document.createElement('h2'); | ||
header.textContent = `${length}-Letter Words`; | ||
section.appendChild(header); | ||
|
||
const wordList = document.createElement('ul'); | ||
words.forEach(word => { | ||
const listItem = document.createElement('li'); | ||
listItem.textContent = word; | ||
wordList.appendChild(listItem); | ||
}); | ||
section.appendChild(wordList); | ||
resultsContainer.appendChild(section); | ||
} | ||
} | ||
|
||
document.getElementById('unscrambleButton').addEventListener('click', descrambleWords); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters