lunes, 17 de julio de 2023

Get a list of unique words sorted alphabetically from drag txt file

 const result = document.evaluate('/html/body/pre', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
const preElement = result.singleNodeValue;
const content = preElement ? preElement.textContent.trim() : '';

// Split the content into an array of words
const words = content.split(/\s+/);

// Create a Set to store unique words
const uniqueWords = new Set(words);

// Convert the Set back to an array and sort it alphabetically
const sortedWords = Array.from(uniqueWords).sort();

console.log('Content:', content);
console.log('Word Count:', words.length);
console.log('Unique Words:', sortedWords);

// Open a new tab with the sorted words
const newTab = window.open('');
newTab.document.write('<html><head>');
newTab.document.write('<style>body { font-family: Consolas; font-size: 10pt; }</style>');
newTab.document.write('</head><body>');
newTab.document.write('<h1>Sorted Words</h1>');
newTab.document.write('<ul>');
sortedWords.forEach((word) => {
  newTab.document.write(`<li>${word}</li>`);
});
newTab.document.write('</ul>');
newTab.document.write('</body></html>');
newTab.document.close();


No hay comentarios:

Publicar un comentario