-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode.js
More file actions
62 lines (49 loc) · 1.69 KB
/
code.js
File metadata and controls
62 lines (49 loc) · 1.69 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
//Cryptarithmetic problem solver
//Uses a bruteforce approach. Nothing fancy
//Makes use of WebWorkers to offload the bulk of the number crunching to a separate non-UI thread
//Author : globalpolicy
//Date : September 29, 2020 | 08:16 AM
"use strict";
function calc(){
let timeout=10000; //timeout of 10 seconds for the search
let word1=document.getElementById("word1").value;
let word2=document.getElementById("word2").value;
let word3=document.getElementById("word3").value;
clearResultText(); //clear past result if any
var worker=new Worker("worker.js");
worker.postMessage([word1,word2,word3,timeout]);
worker.onmessage=function(e){
switch(e.data[0].type){
case "success":
let output=e.data[0].result;
updateSuccessfulResult(output);
worker.terminate();
break;
case "failure":
updateFailureResult();
worker.terminate();
break;
case "progress":
updateProgress(e.data[0].iteration,e.data[0].timeElapsed);
break;
}
}
}
function clearResultText(){
document.getElementById("result").innerHTML="";
}
function updateFailureResult(){
document.getElementById("result").innerHTML="Couldn't find the combination. :(";
}
function updateSuccessfulResult(output){
let divElement=document.getElementById("result");
let outputText=`Combination found! :) <br/>
${output.number1} + ${output.number2} = ${output.number3} <br/>
${output.letterlist} <br/>
${output.digitlist}`;
divElement.innerHTML=outputText;
}
function updateProgress(iterations,elapsedTimeMs){
document.getElementById("iterations").innerHTML=`${iterations} iterations done <br/>
${(elapsedTimeMs/1000).toFixed(3)}s elapsed`;
}