Skip to content

Instantly share code, notes, and snippets.

Created April 28, 2017 23:26
Show Gist options
  • Save anonymous/4ad781df12cc2aef63393242330d1644 to your computer and use it in GitHub Desktop.
Save anonymous/4ad781df12cc2aef63393242330d1644 to your computer and use it in GitHub Desktop.
fresh block
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
body {
font-family: "Railway", "Courier", sans-serif;
margin-left: 30px;
margin-top: 40px
}
#answers {
position: absolute;
top: 120px;
font-size: 30px;
}
#wrongStatement {
position: absolute;
top: 190px;
}
#wrong {
color: red;
}
</style>
<p>Let's play hangman!</p>
<input type="text" id="guess" placeholder="enter a letter" />
<button name="submit" id="submit">submit guess</button>
<p id="output"></p>
<p id="wrongStatement">Wrong guesses: <span id="wrong"></span></p>
<p id="answers"></p>
</head>
<body>
<script>
'use strict';
var goal_word = 'visualization';
var outputMessage = document.getElementById("output")
var submit = document.getElementById("submit")
var guess = document.getElementById("guess")
var wrong = document.getElementById("wrong")
submit.addEventListener("click", function(event) {
onSubmit(guess.value)
})
function updateOutput(message) {
outputMessage.innerHTML = message
}
function setUpDataStore(goal_word) {
var goal_array = goal_word.split("");
var current_answer_array = []
for(var i in goal_array){
if(goal_array[i] == " "){
current_answer_array.push(" ")
} else {
current_answer_array.push("_")
}
}
var wrongGuesses = [];
var rightGuesses = [];
var myData = function() {
return ""
}
myData.updateWrong = function(x) {
wrongGuesses.push(x);
wrong.innerHTML = wrongGuesses.join()
}
function updateFoundLetters(){
answers.innerHTML = current_answer_array.join(" ")
}
function didYouWin(){
console.log(current_answer_array.indexOf("_"))
if(current_answer_array.indexOf("_") == -1){
console.log('no more letters')
return true
} else {
return false;
}
}
myData.updateRight = function(x) {
goal_array.forEach(function(goal,i){
if(goal == x){
current_answer_array[i] = x;
}
})
if(didYouWin()){
updateOutput("You won!")
}
updateFoundLetters();
rightGuesses.push(x);
}
myData.isInGoalWord = function(x) {
if (goal_array.indexOf(x) > -1) {
return true
} else {
return false
}
}
myData.isValidLetter = function(x) {
if (x.length > 1) {
updateOutput("please only submit one letter");
return false
} else if (x.match(/[a-z]/i)) {
return true
} else {
return false
}
}
myData.hasBeenChecked = function(x) {
if (wrongGuesses.indexOf(x) > -1 || rightGuesses.indexOf(x) > -1) {
return true;
}
return false;
}
updateFoundLetters()
return myData;
}
var dataStore = setUpDataStore(goal_word);
function onSubmit(letter) {
if (dataStore.isValidLetter(letter)) {
// check if letter has been checked before
// if yes, change output to error message, if no go on
if (dataStore.hasBeenChecked(letter)) {
updateOutput("You've already guessed the letter " + letter)
} else {
// check if output is in the word
if (dataStore.isInGoalWord(letter)) {
updateOutput("Congrats! The letter " + letter + " is in the answer")
dataStore.updateRight(letter)
} else {
updateOutput("You missed. The letter " + letter + " is not in the word")
dataStore.updateWrong(letter)
}
}
}
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment