mmg/static/math.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2013-04-23 23:27:30 -07:00
function update_board(f, o, s, score) {
2013-04-23 22:48:16 -07:00
$("#first").text(f);
$("#operation").text(o);
$("#second").text(s);
2013-04-23 23:27:30 -07:00
$("#score").text(score);
2013-04-23 22:48:16 -07:00
}
function new_problem() {
2014-12-07 21:23:31 -08:00
var type = document.URL.split('/')[3];
$.get("/api/v0/"+type+"/problem/", function(d) {
2013-05-08 23:41:59 -07:00
update_board(d["First"], d["Operation"], d["Second"], d["Score"]);
2013-04-23 22:48:16 -07:00
$("#answer").val("");
})
}
function deal_with_answer(e) {
var data = {
"first": $("#first").text(),
"operation": $("#operation").text(),
"second": $("#second").text(),
"answer": $("#answer").val(),
};
$.post("/api/v0/attempt/", data, function(d) {
2013-05-09 00:37:35 -07:00
$("#score").text(d["Score"]);
if(d["Status"]) {
2013-04-23 23:06:24 -07:00
$("body").removeClass("wrong");
2013-04-23 22:48:16 -07:00
new_problem();
}
else {
2013-04-23 23:06:24 -07:00
$("body").addClass("wrong");
$("#answer").val("");
2013-04-23 22:48:16 -07:00
}
2013-04-23 23:06:24 -07:00
$("#answer").focus();
2013-04-23 22:48:16 -07:00
});
}
$(function() {
new_problem();
2013-04-23 23:06:24 -07:00
$("#answer").keypress(function(e) {
2013-05-09 00:49:04 -07:00
if (e.which == 13 || e.which == 32) {
2013-04-23 23:06:24 -07:00
deal_with_answer(e);
}
});
2013-04-23 22:48:16 -07:00
});