1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00
elinks/test/ecmascript/arithmetics.html
2022-08-25 22:15:28 +02:00

176 lines
4.0 KiB
HTML

<head>
<title>
ARITHMETICS
</title>
<style>
* {
font-size: 24px;
}
body {
background: black;
}
input {
font-family: monospace;
background: black;
color: gray;
border: 0px;
}
*:focus {
outline: none !important;
border: 0px;
}
</style>
</head>
<body>
<font color="gray">
<form id="core" name="core" action="arithmetics.html">
<pre>
<font color="yellow">#x#</font>
+<font color="yellow">#z#</font>
---------
= <input autocomplete="off" autofocus name="i" value="" size="2">
Control: <font color="#bbbbbb">#f#</font>
Score: <font color="green">#s#</font>
<input name="tm" value="#NOW#" size="3" type="hidden">
<input type="submit" size="2" value="o">
</pre>
</form>
<script>
// CHANGE THE MAX_NUMBER HERE
var MAX_NUMBER=10;
function lpad(number,sz) {
if (sz==undefined) {
sz=4;
}
var maxnum=Math.pow(10,sz)-1;
var str_shift=Array(sz).join(" ");
if (number<=maxnum) { number = (str_shift+number).slice(-sz); }
///console.log(number+" "+maxnum+" "+sz+"|"+str_shift+"|");
return number;
}
function doc_replace(needle,heystack) {
if (document.replace) {
document.replace(needle,heystack);
} else {
document.getElementsByTagName("html")[0].innerHTML=document.getElementsByTagName("html")[0].innerHTML.replace(needle,heystack);
}
}
function findGetParameter(paramName) {
var res = null;
var loc=document.location.href;
//console.log(loc);
var elems=loc.split('?')[1];
if (!elems) { return(res); }
//console.log(elems);
var par=elems.split('&');
for (var i=0;i<par.length;i++) {
var key=par[i].split('=');
if (key[0]==paramName) {
res=key[1];
break;
//console.log("key: "+key[0]+" val: "+key[1]);
}
}
return(res);
}
function proc_var(prnname,lodname,savename,saveval) {
var x = null;
if (lodname!=null) {
x = localStorage.getItem(lodname);
//console.log("Loading "+lodname+" val: "+x);
} else {
x=saveval;
}
if (x==null || !x) { x="@"+prnname+"@"; }
doc_replace('#'+prnname+'#',lpad(x));
if (savename!==undefined) {
localStorage.setItem(prnname,saveval);
//console.log("Saving "+prnname+" val: "+saveval);
}
return(x);
}
function get_rand_var(xname) {
var x = Math.round(Math.random()*(MAX_NUMBER));
localStorage.setItem(xname,x);
doc_replace('#'+xname+'#',lpad(x));
return(x);
}
function prepare_task() {
// print and get input begin
// get previous random number
var a=proc_var('a','x');
var z=proc_var('u','z');
// generate random number
var x=get_rand_var('x');
var y=get_rand_var('z');
// get previous user input
var p=proc_var('p','i');
return [a,z];
}
function get_user_input() {
// get user input
var i = findGetParameter("i");
proc_var('i',null,'i',i);
// print and get input end
return(i);
}
function evaluate_result(a,z,i) {
// feedback loop begin
var correctAnswer=0;
//console.log("z: " + z + " a: " + a + " i: " + i);
if (Number(z) + Number(a) == Number(i)) { // user inputs prev number
doc_replace('#f#',' OK');
doc_replace('#bbbbbb','#00ee00');
correctAnswer=1;
} else {
doc_replace('#f#','NOK');
doc_replace('#bbbbbb','#ee0000');
}
// feedback loop end
return(correctAnswer);
}
function updateScore(gain) {
var keyname="score";
var score=localStorage.getItem(keyname);
if (!score || score==null
|| typeof(score)=="NaN"
|| score=="NaN"
) {
score=0;
}
score=Number(score);
score+=gain;
localStorage.setItem(keyname,score.toString());
return(score);
}
function process_result(cor) {
var score=updateScore(cor);
doc_replace('#s#',lpad(score,6));
}
function get_timestamp() {
var now = new Date();
var res = now.toISOString().replace(/\D/g, "");;
return(res);
}
function prevent_form_resubmission() {
var ts=get_timestamp();
document.replace('#NOW#',ts);
}
prevent_form_resubmission();
var res=prepare_task();
var inp=get_user_input();
var cor=evaluate_result(res[0],res[1],inp);
process_result(cor);
</script>
</font>
</body>
</html>