そろばんの四則演算アプリ
下のボタンを押すと、20個のそろばんの四則演算問題が自動的に作成されます。
問題
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>そろばんの四則演算アプリ</title>
</head>
<body>
<h1>そろばんの四則演算アプリ</h1>
<p>下のボタンを押すと、20個のそろばんの四則演算問題が自動的に作成されます。</p>
<button onclick="createQuestions()">問題を作成する</button>
<h2>問題</h2>
<div id="questions"></div>
<script>
function createQuestions() {
var questionsDiv = document.getElementById("questions");
questionsDiv.innerHTML = "";
for (var i = 0; i < 20; i++) {
var a = Math.floor(Math.random() * 100);
var b = Math.floor(Math.random() * 100);
var c = Math.floor(Math.random() * 100);
var operator1 = "+";
var operator2 = "-";
var operator3 = "×";
var operator4 = "÷";
var operator = "";
var answer;
switch (Math.floor(Math.random() * 4) + 1) {
case 1:
operator = operator1;
answer = a + b + c;
break;
case 2:
operator = operator2;
answer = a - b - c;
break;
case 3:
operator = operator3;
answer = a * b * c;
break;
case 4:
operator = operator4;
answer = a / b / c;
break;
}
var questionText = (i + 1) + ". " + a + " " + operator + " " + b + " " + operator + " " + c + " = ?";
var questionDiv = document.createElement("div");
questionDiv.textContent = questionText;
questionsDiv.appendChild(questionDiv);
}
}
</script>
</body>
</html>