about summary refs log tree commit diff
path: root/script.js
diff options
context:
space:
mode:
authorSaksham Mittal <gotlouemail@gmail.com>2022-04-03 18:23:24 +0530
committerSaksham Mittal <gotlouemail@gmail.com>2022-04-03 18:23:24 +0530
commit44b8cc5299403e58a6e58db1cbc6520065694a7a (patch)
treecf52c858a69210d41d3b78025291ada616e296e6 /script.js
parent19baa3e236c0da1311bf6e79dbfe14eff27c5f2d (diff)
downloadpurple-44b8cc5299403e58a6e58db1cbc6520065694a7a.tar.gz
Get random word from word list, clean up code to work with async
There is still a lot of work left to do, particularly with variables
being passed to certain functions.

No code exists right now where entry by user is checked against the
loaded word list.
Diffstat (limited to 'script.js')
-rw-r--r--script.js60
1 files changed, 39 insertions, 21 deletions
diff --git a/script.js b/script.js
index ad6137e..0ec8a02 100644
--- a/script.js
+++ b/script.js
@@ -1,20 +1,12 @@
-let maxtries = 6;
-
-let input = document.querySelector('input');
-let win = document.querySelector('#win');
-let hint = document.querySelector('#hint');
-let attempt = document.querySelector('#attempt');
-
 let exit = false;
-let tries = 0;
+let url = "words";
 
-let answer = "hello";
-let len = answer.length;
+fetch(url)
+.then(response => response.text())
+.then(wordlist => wordlist.split('\n'))
+.then(wordlist => main(wordlist));
 
-input.setAttribute("maxlength", len);
-hint.innerHTML = "Word is of length: " + len + "</br>";
-
-function letterinstr(c) {
+function letterinstr(c, len, answer) {
 	
 	let isin = false;
 
@@ -29,13 +21,13 @@ function letterinstr(c) {
 
 }
 
-function addGuessDisplay(answer, guess) {
+function addGuessDisplay(answer, guess, len, hint) {
 	
 	let cat = ""
 	for (var i = 0; i < len; i++) {
-		if (guess.charAt(i) != answer.charAt(i) && !(letterinstr(guess.charAt(i))) ) {
+		if (guess.charAt(i) != answer.charAt(i) && !(letterinstr(guess.charAt(i), len, answer)) ) {
 			cat += '<span style="color: #595959">' + guess.charAt(i) + "</span>";
-		} else if (guess.charAt(i) != answer.charAt(i) && (letterinstr(guess.charAt(i))) ) {
+		} else if (guess.charAt(i) != answer.charAt(i) && (letterinstr(guess.charAt(i), len, answer)) ) {
 			cat += '<span style="color: #bebe00">' + guess.charAt(i) + "</span>";
 		} else if (guess.charAt(i) == answer.charAt(i)) {
 			cat += '<span style="color: #00ff00">' + guess.charAt(i) + "</span>";
@@ -45,7 +37,7 @@ function addGuessDisplay(answer, guess) {
 	
 }
 
-function validateInput(str) {
+function validateInput(str, len) {
 	
 	let isvalid = true;
 	
@@ -61,13 +53,13 @@ function validateInput(str) {
 
 }
 
-function checkAnswer(event) {
+function checkAnswer(event, input, input, win, hint, attempt, tries, answer, len, maxtries) {
 	
 	event.preventDefault();
 
 	if (!exit) {
-		if (validateInput(input.value)) {
-			addGuessDisplay(answer, input.value);
+		if (validateInput(input.value, len)) {
+			addGuessDisplay(answer, input.value, len, hint);
 			tries++;
 			if (input.value == answer) {
 				win.innerHTML = "You won!";
@@ -86,3 +78,29 @@ function checkAnswer(event) {
 		}
 	}
 }
+
+function main(wordlist) {
+
+	let maxtries = 6;
+	
+	let form = document.querySelector('form');
+	let input = document.querySelector('input');
+	let win = document.querySelector('#win');
+	let hint = document.querySelector('#hint');
+	let attempt = document.querySelector('#attempt');
+	
+	let tries = 0;
+
+	let wordlen = wordlist.length;
+	let randomword = Math.floor(Math.random() * wordlen);
+
+	let answer = wordlist[randomword];
+	console.log(answer);
+	let len = answer.length;
+	
+	input.setAttribute("maxlength", len);
+	hint.innerHTML = "Word is of length: " + len + "</br>";
+	
+	form.addEventListener("submit", function() { checkAnswer(event, input, input, win, hint, attempt, tries, answer, len, maxtries, wordlist); });
+
+}