-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
79 lines (71 loc) · 3.82 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!doctype html>
<html>
<head>
<title>Pop Quiz</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
<script src="https://code.angularjs.org/1.6.1/angular-sanitize.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Barrio|Advent Pro" rel="stylesheet">
</head>
<body ng-app="myApp" ng-cloak>
<div class="container-fluid">
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<div id="trivia-container" ng-controller="myController">
<h2 id="quiz-title">Pop Quiz</h2>
<div class="navigation row">
<div class="col-md-4"><span class="option"><h4><span class="option-label">Category</span></br> <span class="align-bottom">{{question.category}}</span></h4></span></div>
<div class="col-md-4"><span class="option"><h4><span class="option-label">Difficulty</span></br> {{question.difficulty}}</h4></span></div>
<div class="col-md-4"><span class="option"><h4><span class="option-label">Score</span></br> {{score}}</h4></span></div>
</div>
<h1 ng-bind-html=question.question></h1>
<ul>
<li ng-repeat="answer in question.answers" ng-click="correctGuess || checkResult(answer.isCorrect)">
<span ng-class="{correctGuess: correctGuess}" ng-hide="correctGuess && !answer.isCorrect" ng-bind-html=answer.guess></span>
</li>
</ul>
<div id="bottom-nav">
<h2>{{result}}</h2>
<button class="hvr-forward" ng-show="correctGuess" ng-click="fetchQuestion()">Gimme another</button>
</div>
</div>
</div>
<div class="col-md-2"></div>
</div>
</div>
<script>
var myApp = angular.module('myApp', ['ngSanitize']);
myApp.controller('myController', ['$scope', '$filter', '$http', '$sanitize', function($scope, $filter, $http, $sanitize) {
$scope.score = 0;
$scope.fetchQuestion = function() {
$scope.result = '';
$scope.correctGuess = false;
$http.get('https://opentdb.com/api.php?amount=1').then(function(response) {
var results = response.data.results;
$scope.question = results.map(function(element) {
var answers = element.incorrect_answers.map(function(answer) {
return {guess: answer, isCorrect: false};
});
answers.push({guess: element.correct_answer, isCorrect: true});
answers = _.shuffle(answers);
return {question: element.question, category: element.category, difficulty: element.difficulty, answers: answers};
})[0];
});
}
$scope.checkResult = function(isCorrect) {
if (isCorrect) {
$scope.correctGuess = true;
$scope.score += 1;
$scope.result = 'That\'s correct!';
} else {
$scope.result = 'Nope! Try again...';
}
};
$scope.fetchQuestion();
}]);
</script>
</body>
</html>