-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscripts.js
More file actions
executable file
·232 lines (196 loc) · 6.88 KB
/
scripts.js
File metadata and controls
executable file
·232 lines (196 loc) · 6.88 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// wolfram api will be used to verify that a solution exists
// and to find a valid solution
let SOLVEDthing = ''
function _arrayBufferToBase64( buffer ) {
var binary = '';
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
}
// from mdn documentation
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
// ex. 6 plus 5 returns 6 + 5
function makeProblemReadable(problem) {
console.log(problem)
console.log(typeof(problem))
problem = problem.replace(/plus/g, "+")
problem = problem.replace(/minus/g, "-")
problem = problem.replace(/divided by/g, "/")
problem = problem.replace(/multiplied by/g, "x")
console.log("making readable")
console.log(problem)
return problem
}
// write to file
function WriteFile(path, solution) {
// "c:\\MyFile.txt"
var fh = fopen(path , 3); // Open the file for writing
if(fh!=-1) // If the file has been successfully opened
{
// var str = "Some text goes here...";
fwrite(fh, solution); // Write the string to a file
fclose(fh); // Close the file
}
}
// ucs-2 string to base64 encoded ascii
function utoa(str) {
return window.btoa(unescape(encodeURIComponent(str)));
}
// use wolfram api to VERIFY
// Wolfram|Alpha Fast Query Recognizer API
async function verify(value) {
return true
console.log("verifying")
// URL for queries
let URL = `https://cors-escape.herokuapp.com/http://www.wolframalpha.com/queryrecognizer/query.jsp?appid=L3KTPE-UYAAY8W3TG&mode=Default&i=${value}&output=json`
// did not use fetch because it is not compatible on all browsers
// attempted to use xml http requests
// switched to axios because of its compatibility and good documentation
// found = false
let found = await axios({
method:'get',
url: URL,
responseType:'text'
})
.then(function(response) {
// response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
if (typeof response.data !== 'undefined' && typeof response.data !== 'string') {
// if (response.data.query[0].domain === 'math')
// found = response.data.query[0].accepted
// console.log("query accepted value: ")
console.log(response.data.query[0].accepted)
// return response.data.query[0].accepted
let responseResult = response.data.query[0].accepted
if (responseResult == 'true') {
return true
}
} else {
return false
}
});
console.log("end of verify function")
// return found
return found
}
// use wolfram api to SOLVE
// Wolfram|Alpha Simple API
async function solve(value) {
console.log("SOLVING")
console.log(value)
let URL = `https://cors-escape.herokuapp.com/http://api.wolframalpha.com/v1/simple?appid=L3KTPE-UYAAY8W3TG&i=${value}%3F`
let solution = await axios({
method:'get',
url: URL,
responseType:'arraybuffer'
})
.then(function(response) {
// response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
console.log(response.data)
return response.data
});
return solution
}
// making equations and pushing to html
// make simple add/subtract equation
// generate equations of different levels
function makeEquation(){
let max = 100
let num = getRandomInt(max)
let patterns = 3
if (num%patterns == 0) {
return pattern1()
}
if (num%patterns == 1) {
return pattern2()
}
if (num%patterns == 2) {
return pattern3()
}
return pattern1()
}
async function verifyEquation(eqToVerify) {
console.log(eqToVerify)
let verificationResult = await verify(eqToVerify)
console.log(verificationResult)
console.log(verificationResult == true)
if (verificationResult == true){
console.log("I AM TRUE LET ME GOOOOO")
return eqToVerify
} else {
let newResult = await verifyEquation(makeEquation())
return newResult
}
}
// add equation to html page
async function printSimpleProblem(){
// let problem = await generateSimpleProblem()
let problem = await verifyEquation(makeEquation())
// console.log(await solve(problem))
temp = makeProblemReadable(problem)
document.getElementById('problem-string').innerText = `Solve ${temp}.`
// WriteFile(solutionPath, solutionDict[problem])
solutionDict[problem] = await solve(problem)
SOLVEDthing = solutionDict[problem]
console.log("PRINTING IN PROBLEM CREATION")
console.log(solutionDict[problem])
}
function printSimpleSolution(){
console.log("print simple solution function")
//let problem = (document.getElementById('problem-string').innerText).replace('Solve ', '')
let dkey = document.getElementById('problem-string').innerText
dkey = dkey.replace('Solve ', '')
dkey = dkey.replace('.','')
// solution = solutionDict[dkey]
// solution = solutionDict[Object.keys(solutionDict)[0]]
solution = SOLVEDthing
// solution = _arrayBufferToBase64(solution)
// console.log(solutionDict[problem])
// console.log(solution)
// let htmlInput = `<img src="${solutionPath}">`
// convert solution to base64 string
console.log("PRINTING IN SIMPLE SOLUTION")
console.log(solution)
let codedSolution = _arrayBufferToBase64(solution)
console.log(codedSolution)
let htmlInput = `<img src="data:image/gif;base64, ${codedSolution}">`
// testing purposes
// htmlInput = `<h2> THIS IS A SOLUTION </h2>`
// ends, delete after errors fixed
htmlFull = `<div id = "simple-solution">${htmlInput}</div>`
document.getElementById('results').innerHTML += htmlFull
document.getElementById('show-solution').innerText = "hide solution"
}
function hideSimpleSolution() {
element = document.getElementById('simple-solution')
console.log(element)
element.parentNode.removeChild(element)
document.getElementById('show-solution').innerText = "show solution"
}
window.onload = function() {
let showingSolution = false
printSimpleProblem()
// check for click events
document.getElementById("simple").onclick = function(e){
SOLVEDthing = ''
if (showingSolution == true){
hideSimpleSolution()
showingSolution = false
}
printSimpleProblem()
}
document.getElementById("show-solution").onclick = function(e){
console.log(showingSolution)
if (showingSolution === false){
printSimpleSolution()
showingSolution = true
} else {
hideSimpleSolution()
showingSolution = false
}
}
}