From d1ebeadcbac807fb162575814c3adfd8d03d2675 Mon Sep 17 00:00:00 2001 From: chelsejw Date: Fri, 20 Mar 2020 08:56:23 +0800 Subject: [PATCH] add basic gmae logic part 1-2 --- script.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/script.js b/script.js index 3df1217..af2be01 100644 --- a/script.js +++ b/script.js @@ -1,6 +1,49 @@ -console.log("hello script js"); +var secretWord = ['c', 'a', 't'] +var correct = []; +var totalGuesses = []; +var incorrect = [] +var status; +var added = false; +var emoji = ["(", "╯", "ರ", "~", "ರ", ")", "╯", "︵", "┻", "━", "┻"] +var tableflip = []; -var inputHappened = function(currentInput){ - console.log( currentInput ); - return "WOW SOMETHING HAPPEND"; +document.getElementById(`output`).innerText = `To guess the word, please enter a single letter at a time.` + +function clearInput() { + document.getElementById('input').value = ""; +} +var inputHappened = function(currentInput) { + var output; + clearInput(); + //Define output statement format. + //If user did not enter only one letter, end the function here. + if (currentInput.length !== 1) { + return output = `Sorry, please only enter one letter at a time.` + } + + totalGuesses.push(currentInput); + //If the currentInput matches a letter in secretWord, add it to the correct array. + for (var i = 0; i < secretWord.length; i++) { + if (currentInput === secretWord[i]) { + correct.push(currentInput); + status = `You have guessed correctly.`; + added = true; + } + } + //If no letters got added to the correct array, add letter to incorrect array. + if (added === false) { + incorrect.push(currentInput); + tableflip.push(emoji.shift()); + status = `You have guessed incorrectly.`; + } + //Check if user has either won or lost the game. + if (correct.length === secretWord.length) { + return output = `Congratulations, you have guessed the word: ${secretWord.join("")}!` + } else if (tableflip.length === 10) { + return output = `${tableflip.join("")} You lost!` + } + //Reset the status of whether letters were added. + added = false; + return output = `${status} Your total correct guesses so far: ${correct.join(", ")}. Your incorrect guesses so far: ${incorrect.join(", ")}. Table flip status: ``${tableflip.join("")}`; +; };