Created
June 3, 2014 22:56
-
-
Save radcliff/e5f4e3f7c07a5ce2e333 to your computer and use it in GitHub Desktop.
Hangman in AngularJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://jashkenas.github.io/underscore/underscore-min.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script> | |
<script src="script.js"></script> | |
<meta charset="utf-8"> | |
<title>HangMan</title> | |
</head> | |
<body ng-app> | |
<div ng-controller="HangManCtrl"> | |
<form ng-submit="addWord()"> | |
<input type="password" name="newWord" ng-model="newWord" placeholder="Enter A New Word"> <br> | |
<button> Add Word</button> | |
</form> | |
<span ng-repeat="char in displayChars() track by $index"> | |
{{char}} | |
</span> | |
<form ng-submit="checkGuess()"> | |
<input type="text" name="guess" ng-model="guess"><button>Guess</button> | |
</form> | |
<div ng-pluralize count="guessCount" when="{'0': 'No guesses.', 'one': '1 guess.', 'other': '{} guesses.'}"> | |
{{guessCount}} | |
</div> | |
<div ng-repeat="character in guessedChars track by $index"> | |
{{character}} | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var HangManCtrl = function($scope) { | |
$scope.currentWord = "angular"; | |
$scope.initialize = function(){ | |
$scope.guessedChars = []; | |
$scope.guessCount = 0; | |
}; | |
$scope.addWord = function(){ | |
$scope.currentWord = $scope.newWord; | |
$scope.newWord = ""; | |
$scope.initialize(); | |
}; | |
$scope.checkGuess = function(){ | |
if($scope.guessedChars.indexOf($scope.guess) === -1){ | |
$scope.guessedChars.push($scope.guess); | |
} | |
$scope.guessCount++; | |
$scope.guess = ""; | |
}; | |
$scope.displayChars = function(){ | |
var chars = $scope.currentWord.split(""); | |
_.each(chars, function(val, index){ | |
if( $scope.guessedChars.indexOf(val) === -1){ | |
chars[index] = "_"; | |
} | |
}); | |
return chars; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment