-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate_3.html
89 lines (81 loc) · 3.23 KB
/
calculate_3.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Calculation</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.result {
margin-top: 20px;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.calculation {
border: 1px solid #ccc;
padding: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<h1>Random Calculation</h1>
<label>
<input type="checkbox" id="showAnswers" onclick="toggleAnswers()"> Show Answers
</label>
<button onclick="generateCalculations()">Generate 480 Calculations</button>
<div class="result" id="result"></div>
<script>
let calculations = [];
function generateCalculations() {
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = ''; // Clear previous results
calculations = []; // Reset calculations array
for (let i = 0; i < 480; i++) {
const num1 = Math.floor(Math.random() * 900) + 100;
const num2 = Math.floor(Math.random() * 900) + 100;
const num3 = Math.floor(Math.random() * 900) + 100;
const operations = ['+', '-'];
let result;
let expression = `${num1}`;
// Randomly choose operations and calculate
for (let j = 0; j < 2; j++) {
const operation = operations[Math.floor(Math.random() * operations.length)];
if (operation === '+') {
result = (result || num1) + (j === 0 ? num2 : num3);
expression += ` + ${(j === 0 ? num2 : num3)}`;
} else {
result = (result || num1) - (j === 0 ? num2 : num3);
expression += ` - ${(j === 0 ? num2 : num3)}`;
}
}
// Ensure the result is not negative
if (result < 0) {
result = Math.abs(result);
expression = expression.replace('-', '+');
}
calculations.push({ expression, result });
const calculationDiv = document.createElement('div');
calculationDiv.className = 'calculation';
calculationDiv.innerText = expression;
resultDiv.appendChild(calculationDiv);
}
}
function toggleAnswers() {
const showAnswers = document.getElementById('showAnswers').checked;
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = ''; // Clear previous results
calculations.forEach(calc => {
const calculationDiv = document.createElement('div');
calculationDiv.className = 'calculation';
calculationDiv.innerText = showAnswers ? `${calc.expression} = ${calc.result}` : calc.expression;
resultDiv.appendChild(calculationDiv);
});
}
</script>
</body>
</html>