Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div ng-app="app">
	<form ng-controller="GameCtrl" name="mathGameForm" class="game" ng-submit="checkAnswer()">
		<h1 class="text-center">Math Frenzy!</h1>
		<p ng-show="playingGame() || noGame()">See how many math problems you can answer correctly in one minute!</p>
		<p ng-class="{'alert-success': timer < 20, 'alert-warning': timer >= 20 && timer < 40, 'alert-danger': timer >= 40 && timer < 60 }" ng-show="playingGame()" class="alert h4 timer" role="alert">{{timer}}</p>	
		<p class="form-group" ng-class="{'has-error': isIncorrect(), 'has-success': isAnswer() }" ng-show="playingGame()">
			<label class="control-label center-block text-center h2">{{variables.x}} {{symbol}} {{variables.y}}</label>
			<input type="text" class="form-control" ng-model="answer" placeholder="Answer" />
		</p>
		<p class="h3" ng-show="endGame()">
			Time is up!</p>
		<p ng-show="endGame()">
			You answered {{correct}} out of {{correct+wrong}} correctly with an accuracy rating of {{accuracy() | number }}%
		</p>
		<div class="progress" ng-show="playingGame() || endGame()">
  		<div class="progress-bar" ng-class="{'progress-bar-success': accuracy() >= 75, 'progress-bar-warning': accuracy() >= 50 && accuracy() < 75, 'progress-bar-danger': accuracy() < 50}" role="progressbar" aria-valuenow="accuracy()" aria-valuemin="0" aria-valuemax="100" style="width: {{accuracy()}}%">
    <span>{{correct}} / {{wrong+correct}}</span>
  </div>
</div>
			<p class="text-center" ng-show="noGame() || endGame()">
			 <button type="button" ng-click="startGame()" class="btn btn-primary btn-lg"><span ng-show="noGame()">Start</span><span ng-show="endGame()">New</span> Game</button>
		</p>
	</form>
</div>
              
            
!

CSS

              
                @import "compass/css3";


.game {
	margin: 3em auto;
	padding: 0 10px;
	max-width: 400px;
}

.timer {
	width: 100px;
	text-align: center;
	margin: 0 auto;
}
              
            
!

JS

              
                angular.module('app', []).controller('GameCtrl', ['$scope', '$timeout', function($scope, $timeout){
	$scope.variables = {};
	$scope.correct = 0;
	$scope.wrong = 0;
	$scope.timer = 0;
	_start = false;
	_end = false;
	var _answer = null;
	var _answerCorrect = null;
	var _sym = ['-','+','÷','x'];
	
	function _initialize() {
		$scope.variables = {
			'x': Math.floor(Math.random() * (10)) + 1,	
			'y': Math.floor(Math.random() * (10)) + 1,
		};	
		$scope.symbol = _sym[Math.round(Math.random()*3)];
		switch ($scope.symbol) {
			case '-':
				_answer = $scope.variables.x - $scope.variables.y;
				break;
			case '+':
				_answer = $scope.variables.x + $scope.variables.y;
				break;
			case 'x':
				_answer = $scope.variables.x * $scope.variables.y;
				break;
			default:
				var x = $scope.variables.x;
				$scope.variables.x = x * $scope.variables.y;
				_answer = x;
				break
		}
		_answerCorrect = null;
		$scope.answer = null;
	}
	

	angular.extend($scope, {
		isAnswer: function (){
			return _answerCorrect === true;
		},
		isIncorrect: function (){
			return _answerCorrect === false;
		},
		accuracy: function(){
			return $scope.correct / ($scope.correct + $scope.wrong) * 100;
		},
		noGame: function(){
			return !_start && !_end;
		},
		startGame: function(){
			_start = true;
			_end = false;
			$scope.correct = 0;
			$scope.wrong = 0;
			$scope.timer = 0;
			$timeout($scope.increaseTimer, 1000);
		},
		playingGame: function(){
			return _start && !_end;
		},
		endGame: function() {
			return _start && _end;
		},
		increaseTimer: function(){
			$scope.timer++;
			if($scope.timer == 60){
				_end = true;
			} else {
				$timeout($scope.increaseTimer, 1000);
			}
		},
		checkAnswer: function(){
			_answerCorrect = parseInt($scope.answer) == _answer;
			if(_answerCorrect) {
				$scope.correct++;
				_initialize()
			}else{
				$scope.wrong++;
				$scope.answer = null;
			}
		}
	});
	
	_initialize();
}
																				]);
              
            
!
999px

Console