You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
10 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

$(document).ready(function () {
var results = Array();
var ctime;
var flag_again = false;
var flag_create = false;
var score = 0;
var usetime = 0;
var flag_input = false;
var language = 1;
var module = 0;
var lanconfig = {
"title": ['四则运算生成器', 'Four Operation Generators'],
"module": ['--请选择模式--', '--Module--'],
"create": ['生成题目', 'Generate'],
"exchange": ['English', '简体中文'],
"show": ["查看答案", "Answers"],
"check": ["提交答案", "Submit"],
"cleanall": ['清空题目', 'Clean All']
}
var btn = $("#time");
$("#module").change(function (e) {
e.preventDefault();
module = $(this).val();
console.log("module" + module);
});
$("#create").click(function (e) {
e.preventDefault();
if (!flag_again) {
flag_again = true;
console.log(flag_again);
if (language == 1 && confirm("答题须知除法的商请以分数形式输入示例1 ÷ 2 = 1/2")) {
$("#show").hide();
if (module) {
if (parseFloat(btn.text()) == 0 || btn.text() == "") {
results = [];
score = 0;
usetime = 0;
begin();
} else {
return;
}
} else {
if (language != -1) {
alert("请先选择出题模式。");
} else {
alert("Please Choose Module First!");
}
}
} else if (language == -1 && confirm("Answer note: Please enter the quotient of division as a fraction! Such as: 1 ÷ 2 = 1/2")) {
$("#show").hide();
if (module) {
if (parseFloat(btn.text()) == 0 || btn.text() == "") {
results = [];
score = 0;
usetime = 0;
begin();
} else {
return;
}
} else {
if (language != -1) {
alert("请先选择出题模式。");
} else {
alert("Please Choose Module First!");
}
}
}
} else {
$("#show").hide();
if (module) {
if (parseFloat(btn.text()) == 0 || btn.text() == "") {
results = [];
score = 0;
usetime = 0;
begin();
} else {
return;
}
} else {
if (language != -1) {
alert("请先选择出题模式。");
} else {
alert("Please Choose Module First!");
}
}
}
//console.log(results);
});
$("#exchange").change(function (e) {
e.preventDefault();
language = language * (-1);
var index = $(this).val();
$(".btnn").each(function () {
var _this = $(this);
_this.html(lanconfig[_this.data("name")][index]);
})
if (flag_input && language == 1) {
$("#info").text("用时:" + usetime + "s 得分:" + score);
$("#info").show();
} else if (flag_input && language == -1) {
$("#info").text("Use Time" + usetime + "s Score" + score);
$("#info").show();
} else {
$("#info").hide();
}
if (flag_input) {
check();
}
if (language == -1) {
$("#module_default").text("--Module--");
$("h1").text(lanconfig['title'][index]);
} else {
$("#module_default").text("--请选择模式--");
$("h1").text(lanconfig['title'][index]);
}
});
$("#check").click(function (e) {
e.preventDefault();
if (language != -1) {
if (confirm("确认提交?(提交后不可修改!!!)")) {
stopct();
flag_input = true;
check();
$("#show").show();
$(".result").attr("disabled", 'disabled');
$("#check").attr("disabled", 'disabled');
} else {
return;
}
} else {
if (confirm("Do you confirm submission?(Answers cannot be modified after submission!!!)")) {
stopct();
flag_input = true;
check();
$("#show").show();
$(".result").attr("disabled", 'disabled');
$("#check").attr("disabled", 'disabled');
} else {
return;
}
}
});
$("#show").click(function (e) {
console.log("click");
e.preventDefault();
var inputs = $("input[class='result']");
var notice = $("span[class='notice']");
for (let i = 0; i < results.length; i++) {
if (notice.eq(i).data('right') == '0') {
inputs.eq(i).attr("type", "text");
inputs.eq(i).val(results[i]);
inputs.eq(i).css('color', 'red');
}
}
$(".notice").hide();
stopct();
$("#check").attr("disabled", 'disabled');
$(".result").attr("disabled", 'disabled');
});
$("#stop").click(function (e) {
e.preventDefault();
console.log(ctime);
stopct();
});
$("#cleanall").click(function (e) {
e.preventDefault();
if (flag_create) {
$("#ul").slideToggle("slow");
$("#ul").find("li").remove();
resetct();
$("#info").hide();
$("#check").hide();
flag_create = false;
$("#cleanall").hide();
$("#show").hide();
} else {
resetct();
$("#info").hide();
$("#check").hide();
flag_create = false;
$("#cleanall").hide();
$("#show").hide();
}
});
function begin() {
$.ajax({
type: "get",
url: "/begin",
data: "begin=" + module,
success: function (res) {
let counts = Object.keys(res).length;
for (let i = 0; i < counts; i++) {
$("#ul").append("<li><span class='detials'>" + res[i].formula + " = " + "</span><input class='result' type='text'>" + "<span class='notice'></span></li>");
var tmp = res[i].result;
var reg = /\s+/g;
//console.log(typeof tmp + " " + tmp);
if (typeof tmp == "string") {
tmp = tmp.replace(reg, "");
}
//results.push(res[i].result);
results.push(tmp);
}
$("#ul").slideToggle("slow");
$("#cleanall").show();
count_time();
flag_create = true;
$("#check").removeAttr("disabled", 'disabled');
$("#check").show();
}
}).fail(function () {
begin();
});
}
function count_time(time) {
if (time != undefined) {
var len = time;
} else {
var len = 300;
}
btn = $("#time");
btn.text(len + 's');
ctime = setInterval(function () {
usetime = 300 - (parseFloat(btn.text()) - 1);
btn.text(parseFloat(btn.text()) - 1 + 's');
if (parseFloat(btn.text()) == 0) {
stopct();
flag_input = true;
check();
$("#show").show();
$(".result").attr("disabled", 'disabled');
$("#check").attr("disabled", 'disabled');
}
}, 1000);
}
function check() {
var inputs = $("input[class='result']");
var notices = $("span[class='notice']");
if (flag_input) {
for (let i = 0; i < results.length; i++) {
console.log(inputs.eq(i).val());
if (inputs.eq(i).val() != "" && inputs.eq(i).val() == results[i]) {
if (language != -1) {
notices.eq(i).text("结果正确!");
} else {
notices.eq(i).text("Correct!");
}
notices.eq(i).attr("data-right", "1");
notices.eq(i).css('color', 'green');
notices.eq(i).show();
score++;
} else {
if (language != -1) {
notices.eq(i).text("结果错误!");
} else {
notices.eq(i).text("Wrong!!");
}
notices.eq(i).attr("data-right", "0");
notices.eq(i).css('color', 'red');
notices.eq(i).show();
}
}
if (language == 1) {
$("#info").text("用时:" + usetime + "s 得分:" + score);
$("#info").show();
} else {
$("#info").text("Use Time" + usetime + "s Score" + score);
$("#info").show();
}
} else {
if (language == 1) {
$("#info").text("用时:" + usetime + "s 得分0");
$("#info").show();
} else {
$("#info").text("Use Time" + usetime + "s Score0");
$("#info").show();
}
}
}
function stopct() {
clearInterval(ctime);
}
function resetct() {
stopct();
btn.text("0s");
}
});