Skip to content

Commit 6263b62

Browse files
author
aryan
committed
fixed all Calculator with multiple instances
1 parent a1a7c48 commit 6263b62

3 files changed

Lines changed: 50 additions & 75 deletions

File tree

content/exercises/graded-assignments/mathematics-1/q2.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ excludeSearch: false
4646

4747

4848

49-
<!-- {{< math/calc3 a=10 b=5 formula="a + b * 2" >}} -->
49+
{{< math/calc3 a=10 b=5 formula="a + b * 2" >}}
5050

51-
<!-- {{< math/calc3 x=2 y=3 z=4 formula="x * y + z" >}} -->
51+
{{< math/calc3 x=2 y=3 z=4 formula="x * y + z" >}}
5252

5353
<!-- {{< math/calc3 initialValue=10 growthRate=2 time=5 formula="initialValue * (1 + growthRate/100) ^ time" >}} -->
5454

5555

5656

5757

58-
<!-- {{< math/add_numbers num1="5" num2="10" buttonText="Add Now" >}} -->
58+
{{< math/add_numbers num1="5" num2="10" buttonText="Add Now" >}}
5959

60-
<!-- {{< math/add_numbers num1="5" num2="20" buttonText="Add Now" >}} -->
60+
{{< math/add_numbers num1="5" num2="20" buttonText="Add Now" >}}

layouts/shortcodes/calc.html

Lines changed: 0 additions & 40 deletions
This file was deleted.

layouts/shortcodes/math/calc3.html

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,53 @@
1-
<!-- filepath: d:\Code\hextra-notes\layouts\shortcodes\math\calc3.html -->
2-
{{ $id := printf "calc_%d" now.UnixNano }}
3-
{{ $formula := .Get "formula" | default "a + b" }}
4-
5-
<div id="{{ $id }}">
6-
<label for="formula-{{ $id }}">Formula:</label>
7-
<input type="text" id="formula-{{ $id }}" value="{{ $formula }}">
1+
{{/* Generate a unique ID for this instance */}}
2+
{{- $uid := .Page.Scratch.Get "addCounter" | default 0 -}}
3+
{{- .Page.Scratch.Set "addCounter" (add $uid 1) -}}
84

9-
{{ range $key, $value := .Params }}
10-
{{ if ne $key "formula" }}
11-
<label for="{{ $key }}-{{ $id }}">{{ $key }}:</label>
12-
<input type="number" id="{{ $key }}-{{ $id }}" value="{{ $value }}">
13-
{{ end }}
5+
{{ $formula := .Get "formula" | default "a + b" }}
6+
<div id="calc-{{$uid}}" class="calc-box">
7+
{{ range $key, $value := .Params }}
8+
{{ if ne $key "formula" }}
9+
<label>
10+
{{ $key }}:
11+
<input type="number" id="calc-{{$uid}}-{{$key}}" value="{{ $value }}">
12+
</label>
13+
<br>
1414
{{ end }}
15-
16-
<button onclick="calculate_{{ $id }}()">Calculate</button>
17-
<p>Result: <span id="result-{{ $id }}"></span></p>
15+
{{ end }}
16+
<label>
17+
Formula:
18+
<input type="text" id="calc-{{$uid}}-formula" value="{{ $formula }}">
19+
</label>
20+
<br>
21+
<button type="button" onclick="calcSolve('calc-{{$uid}}')">Solve</button>
22+
<div id="calc-{{$uid}}-result">Result: </div>
1823
</div>
1924

2025
<script>
21-
function calculate_{{ $id }}() {
22-
try {
23-
const formulaInput = document.getElementById('formula-{{ $id }}').value;
24-
const vars = {};
25-
{{ range $key, $value := .Params }}
26-
{{ if ne $key "formula" }}
27-
vars["{{ $key }}"] = parseFloat(document.getElementById('{{ $key }}-{{ $id }}').value) || 0;
28-
{{ end }}
29-
{{ end }}
26+
function calcSolve(boxId) {
27+
const box = document.getElementById(boxId);
28+
const inputs = box.querySelectorAll('input[type="number"]');
29+
let formula = box.querySelector('input[type="text"]').value;
30+
inputs.forEach(input => {
31+
const varName = input.id.split('-').pop();
32+
formula = formula.replace(new RegExp('\\b' + varName + '\\b', 'g'), input.value || '0');
33+
});
34+
try {
35+
// For public sites, use a math parser instead of eval for safety!
36+
const result = eval(formula);
37+
box.querySelector('[id$="result"]').textContent = "Result: " + result;
38+
} catch {
39+
box.querySelector('[id$="result"]').textContent = "Result: Error in formula";
40+
}
41+
}
42+
</script>
3043

31-
const result = new Function(...Object.keys(vars), 'return ' + formulaInput + ';')(...Object.values(vars));
32-
document.getElementById('result-{{ $id }}').textContent = result;
33-
} catch (error) {
34-
document.getElementById('result-{{ $id }}').textContent = "Invalid Formula";
35-
console.error("Error in calculate_{{ $id }}():", error);
36-
}
44+
<style>
45+
.calc-box {
46+
border: 1px solid #ccc; border-radius: 8px; padding: 1em; margin: 1em 0; max-width: 320px;
47+
background: #f9f9f9; font-family: system-ui, sans-serif;
3748
}
38-
</script>
49+
.calc-box label { display: block; margin-bottom: 0.5em; }
50+
.calc-box input { margin-left: 0.5em; }
51+
.calc-box button { margin-top: 0.5em; }
52+
.calc-box div[id$="result"] { margin-top: 0.5em; font-weight: bold; color: #007800; }
53+
</style>

0 commit comments

Comments
 (0)