From 9895f9bd0662367d4d9712e77a17b2f46da6f0c0 Mon Sep 17 00:00:00 2001 From: dannst Date: Thu, 19 Mar 2020 22:11:43 +0800 Subject: [PATCH 1/2] Table working as intended, flips when game over before game restarts itself. If user enters a correct response, game will show correct input. When user completes the secret word before table completes, user will receive a win message. has not entered secret word randomization --- script.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/script.js b/script.js index 3df1217..31bfde6 100644 --- a/script.js +++ b/script.js @@ -1,6 +1,70 @@ console.log("hello script js"); +//variables +let secretWord = ['c', 'a', 't']; +let secretWordList = [ + ['c','a','t'], + ['d','o','g','g','y'], + ['a','l','p','h','a','b','e','t'] + ]; +let completeTable = ['(', '╯', 'ರ', '~', 'ರ', ')', '╯', '︵', '┻━┻']; +let currentTable = []; +let correctEntries = []; +let wrongEntries = []; +let wrongAttempts = 0; + +//helper functions +let restartGame = function(){ + wrongAttempts = 0; + currentTable = []; + correctEntries = []; + wrongEntries = []; +} + +let clearInput = function(){ + document.querySelector('#input').value = ""; +} + +// Takes in a string and an word array; return true if string in word array, false if otherwise +let letterCheck = function(letter, array){ + for (i = 0; i < array.length; i++){ + if (letter === array[i]){ + return true; + }; + }; + return false; +}; + +//remove an existing string from an array of strings +let deleteString = function(string, array){ + let i = array.indexOf(string); + array.splice(i, 1) +} + + var inputHappened = function(currentInput){ - console.log( currentInput ); - return "WOW SOMETHING HAPPEND"; + if (letterCheck(currentInput, secretWord)){ + correctEntries.push(currentInput); + deleteString(currentInput, secretWord) + clearInput(); + console.log(secretWord); + if (secretWord.length === 0){ + return 'You won! The secret word is "cat"!' + } + return `You guessed right. Your correct guesses so far: ${correctEntries} \n\nTable: ${currentTable}`; + } else if (wrongAttempts < completeTable.length-1){ + wrongEntries.push(currentInput); + currentTable += (completeTable[wrongAttempts]); + wrongAttempts += 1; + clearInput(); + return `You guessed wrong. Your correct guesses so far: ${correctEntries} \n\n\Table: ${currentTable}`; + } else if (wrongAttempts === currentTable.length) { + clearInput(); + currentTable += (completeTable[wrongAttempts]); + console.log() + return `You guessed wrong. Your correct guesses so far: ${correctEntries} \n\nTable: ${currentTable} \nGame Over`; + } + restartGame(); + return 'New Game. \nPlease re-enter input'; + }; From ef2f7e3620b5c5ac6112db3e067d32f1e6db500b Mon Sep 17 00:00:00 2001 From: dannst Date: Thu, 19 Mar 2020 23:05:45 +0800 Subject: [PATCH 2/2] added hint function which takes in a secretword, and a user guess. Iterates through the secret word; if the user guess doesnt match secretword's letters, replace letter with "_". --- script.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/script.js b/script.js index 31bfde6..f47082a 100644 --- a/script.js +++ b/script.js @@ -41,7 +41,18 @@ let deleteString = function(string, array){ array.splice(i, 1) } +//hint function showing correct guesses, returns _ for each unknown letter in the word +let hint = function(word, guess){ + for (i = 0, i < word.length < i++){ + if (word[i] !== guess) { + word[i] = '_'; + } + } +} + + +//userInput function var inputHappened = function(currentInput){ if (letterCheck(currentInput, secretWord)){ correctEntries.push(currentInput);