I have a step in my code where the application will check on the word entered by the user and compare it to the a word. My issue is that my original code was javascript and now I am not certain how to set it up for AngularJS. I had to ideas but neither one work. This is my option in the my controller.js
file
$scope.addGuess = function()
{
$scope.guesses.push($scope.guestGuess);
$scope.guessed = $scope.guesses.length;
$scope.correctResult = "You have guessed the correct word. Way to go!";
$scope.incorrectResult = "Please try again!";
if ($scope.guess == $scope.wordToGuess)
{
$scope.result ="You have guessed the correct word";
alert(result);
}
else if ($scope.guess == $scope.wordToGuess)
{
$scope.result = "Please try again!";
alert(result);
}
$scope.guess = '';
}
});
Here is the option I was trying to use on my HTML
file
<p align="center" ng-if="guestGuess == wordToGuess">{{correctResult}}</p>
<p align="center" ng-if="guestGuess != wordToGuess">{{incorrectResult}}</p>
The idea is that the word will be compared an amount of times, say 4 times, after 4 times the user will lose the game, so eventually I have to add a second condition about how many choices are left.
Thank you guys.
Here is the full code
HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="guessGameApp">
<head>
<title>Web Manager - AngularJS</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js" type="text/javascript"></script>
<script src="js/controllers/app.js" type="text/javascript"></script>
<script src="js/controllers/maincontroller.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="css/magicWord.css">
<!--<script src="js/jquery-1.11.0.min.js"></script>-->
</head>
<body>
<div ng-controller="guessGameController">
<p>
<header id="masthead">
<h2 align="center">{{appTitle}}</h2>
</header>
</p>
<div ng-controller="wordController">
<p>
<table align="center" width="300px" height="150px" border="solid 2px">
<tr>
<td id="guessBox">
<p align="center"><input value="" type="text" placeholder="Enter Guess" ng-model="guestGuess" /></p>
<p align="center">Your guess is: {{guestGuess}}</p>
</td>
</tr>
<tr>
<td id="guessBox">
<p align="center"><button ng-click="addGuess()">Click to Check</button></p>
</td>
</tr>
</table>
<p align="center" ng-if="guestGuess == wordToGuess">{{correctResult}}</p>
<p align="center" ng-if="guestGuess != wordToGuess">{{incorrectResult}}</p>
</p>
<p>
<h3>Your guesses so far are: </h3>
<ul>
<li ng-repeat="words in guesses">
<p>{{words}}</p>
</li>
</ul>
<p>You have guessed:<b>{{guessed}}</b> times out {{allowed}} chances.</p>
<p>You have <b>{{allowed - guessed}}</b> guesses left.</p>
</p>
</div>
</div>
</body>
</html>
app.js
var gameApp = angular.module('guessGameApp', []);
maincontroller.js
var gameApp = angular.module('guessGameApp', []);
gameApp.controller("guessGameController", function($scope)
{
$scope.appTitle = "WELCOME TO THE GUESS GAME!";
});
gameApp.controller('wordController', function($scope)
{
$scope.guess = '';
$scope.guesses = [];
$scope.guessed= '';
$scope.allowed = 6;
$scope.wordToGuess = "Just";
$scope.addGuess = function()
{
$scope.guesses.push($scope.guestGuess);
$scope.guessed = $scope.guesses.length;
$scope.correctResult = "You have guessed the correct word. Way to go!";
$scope.incorrectResult = "Please try again!";
if ($scope.guess == $scope.wordToGuess)
{
$scope.result ="You have guessed the correct word";
alert(result);
}
else if ($scope.guess == $scope.wordToGuess)
{
$scope.result = "Please try again!";
alert(result);
}
$scope.guess = '';
}