-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiFunctions.js
More file actions
37 lines (33 loc) · 1.18 KB
/
apiFunctions.js
File metadata and controls
37 lines (33 loc) · 1.18 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
const getRandomPuzzle = () => {
const answerKey = possibleWordKeys[Math.floor(Math.random()*possibleWordKeys.length)];
const requiredLetter = answerKey[Math.floor(Math.random()*answerKey.length)]
const answerWords = [];
const answerKeyArray = answerKey.split('');
answerKeyArray.splice(answerKeyArray.indexOf(requiredLetter), 1);
const answerLetters = answerKeyArray.join('');
const comboArray = getCombinations(answerKeyArray);
comboArray.forEach(combo => {
const comboWords = allKeys[(requiredLetter + combo).split('').sort().join('')];
Array.prototype.push.apply(answerWords, comboWords);
})
return {answerLetters, requiredLetter, answerWords};
}
const getCombinations = (chars) => {
var result = [];
var f = function(prefix, chars) {
for (var i = 0; i < chars.length; i++) {
result.push(prefix + chars[i]);
f(prefix + chars[i], chars.slice(i + 1));
}
}
f('', chars);
return result;
}
ThunkableWebviewerExtension.receiveMessageWithReturnValue(function(message, callback) {
if (message === 'getRandomPuzzle') {
callback(getRandomPuzzle());
} else {
callback(null);
}
});
ThunkableWebviewerExtension.postMessage('pageLoaded');