From 60c68b3209b3c09d3cbd29b4ef33080546a43fbd Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Fri, 15 Mar 2024 11:21:15 -0400 Subject: Weekly Challenge Blast from the Past --- challenge-005/zapwai/javascript/005.html | 21 ++++++++++++ challenge-005/zapwai/javascript/ch-1.js | 50 +++++++++++++++++++++++++++ challenge-005/zapwai/javascript/ch-2.js | 59 ++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 challenge-005/zapwai/javascript/005.html create mode 100644 challenge-005/zapwai/javascript/ch-1.js create mode 100644 challenge-005/zapwai/javascript/ch-2.js (limited to 'challenge-005/zapwai/javascript') diff --git a/challenge-005/zapwai/javascript/005.html b/challenge-005/zapwai/javascript/005.html new file mode 100644 index 0000000000..b613788d30 --- /dev/null +++ b/challenge-005/zapwai/javascript/005.html @@ -0,0 +1,21 @@ + + + + + +

+
+

Anagram Viewer!

+
+ +
+ +
+
+

Hello World

+
+ + + diff --git a/challenge-005/zapwai/javascript/ch-1.js b/challenge-005/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..39a6ccd044 --- /dev/null +++ b/challenge-005/zapwai/javascript/ch-1.js @@ -0,0 +1,50 @@ +let dict; + +const fileInput = document.getElementById('fileInput'); +fileInput.addEventListener('change', handleFileUpload); + +const ourWord = document.getElementById('ourWord'); +ourWord.addEventListener('change', onWordChange); + +const ourParagraph = document.getElementById('ourParagraph'); + +function anagramTime(word) { + let sorted = dict.slice(); + sorted.forEach( (w, i) => { + sorted[i] = w.split('').sort().join(""); + }); + + let sorted_word = word.split('').sort().join(''); + + let anagrams = []; + for (let i = 0; i < dict.length; i++) { + if (sorted[i] == sorted_word) { + anagrams.push(dict[i]); + } + } + ourParagraph.innerHTML = + `Input word: ${word}
Anagrams: ${anagrams.join(' ')}
`; +} + +function onWordChange(event) { + let word = event.target.value; + anagramTime(word); +} + +function handleFileUpload(event) { + const file = event.target.files[0]; + if (!file) { + console.error('No file selected'); + return; + } + const reader = new FileReader(); + reader.onload = function(e) { + const content = e.target.result; + const words = content.split(/\s+/); + dict = words; + let word = document.getElementById('ourWord').value; + anagramTime(word); + }; + reader.readAsText(file); +} + diff --git a/challenge-005/zapwai/javascript/ch-2.js b/challenge-005/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..07ba52c4c2 --- /dev/null +++ b/challenge-005/zapwai/javascript/ch-2.js @@ -0,0 +1,59 @@ +let dict; + +const fileInput = document.getElementById('fileInput'); +fileInput.addEventListener('change', handleFileUpload); + +const ourParagraph = document.getElementById('ourParagraph'); + +function handleFileUpload(event) { + const file = event.target.files[0]; + if (!file) { + console.error('No file selected'); + return; + } + const reader = new FileReader(); + reader.onload = function(e) { + const content = e.target.result; + const words = content.split(/\s+/); + dict = words; + mostAnagrams(); + }; + reader.readAsText(file); +} + +function mostAnagrams() { + let sorted = dict.slice(); + sorted.forEach( (w, i) => { + sorted[i] = w.split('').sort().join(""); + }); + sorted.sort(); + + let maxnum = 0; + let maxword = ""; + + let flag = 0; + let cnt = 0; + for (let i = 0; i < sorted.length; i++) { + if (flag == 1) { + if (sorted[i+1] != sorted[i]) { + flag = 0; + cnt += 1; + if (maxnum < cnt) { + maxnum = cnt; + maxword = sorted[i]; + } + cnt = 0; + } + cnt += 1; + } else { + if (sorted[i + 1] == sorted[i]) { + cnt += 1; + flag = 1; + } + } + } + + ourParagraph.innerHTML = + `Max number of anagrams: ${maxnum}
+Characters: ${maxword}
`; +} -- cgit