forked from CodewarsClone/Codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrainingCtrl.js
More file actions
108 lines (95 loc) · 3.39 KB
/
Copy pathtrainingCtrl.js
File metadata and controls
108 lines (95 loc) · 3.39 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**********TRAINING CONTROLLER************/
angular.module('app').controller('trainingCtrl', function($scope, $state, mainService, $stateParams) {
$scope.kataid = $stateParams.kataid;
$scope.passed = false;
/** Create text areas **/
var textarea1 = document.getElementById('solution-input');
var solutionsCode = CodeMirror.fromTextArea(textarea1, {
lineNumbers: true,
theme: 'seti',
});
var textarea2 = document.getElementById('example-input');
var examplesCode = CodeMirror.fromTextArea(textarea2, {
lineNumbers: true,
theme: 'seti',
});
$scope.languages = ['JavaScript', 'Python'];
$scope.versions = ['Node v0.10.33', 'Node v6.6.0'];
$scope.output = [];
//NG-SHOWS
$scope.showOutputShow = true;
$scope.showInstructionsShow = false;
$scope.showOutput = function() {
$scope.showOutputShow = false;
$scope.showInstructionsShow = true;
}
$scope.showInstructions = function() {
$scope.showOutputShow = true;
$scope.showInstructionsShow = false;
}
// GET KATA INFORMATION
$scope.getKataById = (kataid) => {
mainService.getKataById(kataid).then((response) => {
let examplesTxt = ``;
response.data.examples.forEach((example, i) => {
examplesTxt = examplesTxt + example.test +`\n`
});
console.log(response.data);
$scope.name = response.data.name;
$scope.instructions = response.data.description;
$scope.kyu = response.data.kyu;
$scope.starter = response.data.starter_code;
$scope.examples = examplesTxt;
$scope.kataid = response.data.id;
}).then(() =>{
solutionsCode.setValue($scope.starter);
examplesCode.setValue($scope.examples);
});
}
$scope.getKataById($scope.kataid);
//Examples should be an array of objects. Returned results will be an array with the different tests and their results.
$scope.testExamples = function() {
console.log('test is working Josh');
var solutions = solutionsCode.getValue();
var examples = examplesCode.getValue();
$scope.showOutput();
solutions = solutions.replace(/\n/g, " ");
solutions = solutions.replace(/\s+/g, " ");
var examplesArr = [];
examples = examples.split(/\n/);
examples.forEach(example => examplesArr.push({test: example}));
var t0 = performance.now()
mainService.testExamples(solutions, examplesArr).then((response) => {
$scope.output = [];
response.data.forEach((ele, i) => {
$scope.output.push(ele)
});
console.log(response.data);
});
var t1 = performance.now();
$scope.time = "Time: " + Math.round((t1 - t0)*1000) + " ms";
}
$scope.testSuite = function() {
var solutions = solutionsCode.getValue();
$scope.showOutput();
solutions = solutions.replace(/\n/g, " ");
solutions = solutions.replace(/\s+/g, " ");
mainService.testSuite(solutions, $scope.kataid).then((response) => {
$scope.passed = true
$scope.output = [];
response.data.forEach((ele, i) => {
$scope.output.push(ele)
if (!ele.passed) {$scope.passed = false}
});
console.log(response.data);
});
};
$scope.addPointsToUser = (points, userid) => {
mainService.addPointsToUser(mainService.pointsCalculator($scope.kyu, mainServive.user.id), mainServive.user.id)
}
$scope.submitAnswer = (solution, kataid, userid) => {
if ($scope.passed){
mainService.submitAnswer(solution, kataid, userid);
}
}
});