aboutsummaryrefslogtreecommitdiff
path: root/challenge-005/zapwai/javascript
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-005/zapwai/javascript')
-rw-r--r--challenge-005/zapwai/javascript/005.html21
-rw-r--r--challenge-005/zapwai/javascript/ch-1.js50
-rw-r--r--challenge-005/zapwai/javascript/ch-2.js59
3 files changed, 130 insertions, 0 deletions
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 @@
+<html>
+ <head>
+ <style>
+ body {background-color: lightgray;}
+ </style>
+ </head>
+ <body>
+ <br><br>
+ <center>
+ <p>Anagram Viewer!</p>
+ <form id="uploadForm" enctype="multipart/form-data">
+ <label for="text">Enter a word:</label>
+ <input id="ourWord" type="text"><br>
+ <label for="file">Select a file:</label>
+ <input id="fileInput" type="file"><br>
+ </form>
+ <p id="ourParagraph">Hello World</p>
+ </center>
+ <script src="005-2.js"></script>
+ </body>
+</html>
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}<br>Anagrams: ${anagrams.join(' ')}<br>`;
+}
+
+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}<br>
+Characters: ${maxword}<br>`;
+}