Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<html>
<head>
<meta charset="UTF-8">
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
</head>
<body>
<h1>足し算ジェネレータ</h1>
<div>設問数: <input id="math_gen_count" style="border: 1px solid black;" type="number" value="10" /></div>
<div>最小: <input id="math_gen_min" style="border: 1px solid black;" type="number" value="1" /></div>
<div>最大: <input id="math_gen_max" style="border: 1px solid black;" type="number" value="9" /></div>
<div>演算子: <label><input checked="checked" name="op" type="radio" value="+" />+</label> <label><input name="op" type="radio" value="*" />*</label></div>
<input style="width: 100%; height: 40px; background-color: #007bff; color: white; font-weight: bold;" type="button" value="作る" onclick="makeQuizExec()" />
<textarea id="math_gen_result" style="width: 100%; height: 300px;"></textarea>
<input id="download" style="width: 100%; height: 40px; background-color: #007bff; color: white; font-weight: bold;" type="button" value="ダウンロード" onclick="handleDownload()"/>
<script>
function makeQuiz(min, max, op){
var v1 = makeRandom(min, max);
var v2 = makeRandom(min, max);
var str = "";
str += v1 + " " + op + " " + v2 + " =\n";
str += "sa:\n";
//正答を作る
var answerArray = [];
var answer = calc(v1, v2, op);
answerArray.push(answer);
str += answer + "\n";
//誤答を作る
var dummyCount = 0;
var endlessLoopDetector = 0;
while(dummyCount < 3){
var v3 = makeRandom(min, max);
var v4 = makeRandom(min, max);
var dummyAnswer = calc(v3, v4, op);
if(answerArray.indexOf(dummyAnswer) == -1){
str += dummyAnswer + "\n";
answerArray.push(dummyAnswer);
dummyCount++
}else{
if(endlessLoopDetector++ > 10**3)break;
}
}
return str;
}
function calc(a, b, op){
switch(op){
case "+": return a+b;
case "*": return a*b;
}
}
function makeRandom(min, max){
return Math.floor(min + (max - min + 1) * Math.random());
}
function makeQuizN(min, max, op, n){
var qArray = [];
for(var i = 0; i < n; i++){
qArray.push(makeQuiz(min, max, op));
}
return qArray.join("\n");
}
function makeQuizExec(){
var min = parseInt(jQuery("#math_gen_min").val());
var max = parseInt(jQuery("#math_gen_max").val());
var op = jQuery("[name=op]:checked").val();
var count = parseInt(jQuery("#math_gen_count").val());
jQuery("#math_gen_result").val(makeQuizN(min, max, op, count));
}
function handleDownload(){
var data = jQuery("#math_gen_result").val();
var blob = new Blob([data], {type: "text/plain"});
var a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.target = '_blank';
a.download = 'quiz.txt';
a.click();
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.