Skip to content

Commit 540b962

Browse files
committed
Improved sqrt
1 parent 8029a7d commit 540b962

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

Calc.html

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ <h2 class="history-title">
10741074
<!-- Row 2 -->
10751075
<button class="btn function" onclick="appendFunction('1/')">1/x</button>
10761076
<button class="btn function" onclick="appendOperator('^2')">x²</button>
1077-
<button class="btn function" onclick="appendFunction('sqrt(')">√x</button>
1077+
<button class="btn function" onclick="appendSqrt()">√x</button>
10781078
<button class="btn operator" onclick="appendOperator('/')">÷</button>
10791079
10801080
<!-- Row 3 -->
@@ -1126,7 +1126,7 @@ <h2 class="history-title">
11261126
<button class="btn scientific" onclick="appendOperator('mod')">mod</button>
11271127
11281128
<!-- Row 3 -->
1129-
<button class="btn scientific" onclick="appendFunction('sqrt(')">²√x</button>
1129+
<button class="btn scientific" onclick="appendSqrt()">²√x</button>
11301130
<button class="btn operator" onclick="appendOperator('(')">(</button>
11311131
<button class="btn operator" onclick="appendOperator(')')">)</button>
11321132
<button class="btn scientific" onclick="appendOperator('!')">n!</button>
@@ -1314,6 +1314,24 @@ <h2 class="history-title">
13141314
updateDisplay();
13151315
}
13161316

1317+
function appendSqrt() {
1318+
if (justCalculated) {
1319+
expression = '';
1320+
justCalculated = false;
1321+
}
1322+
1323+
// Check if there's a current value to apply sqrt to
1324+
if (expression && !expression.match(/[\+\-\*\/\^\(]$/)) {
1325+
// If expression ends with a number, wrap it in sqrt
1326+
expression = 'sqrt(' + expression + ')';
1327+
} else {
1328+
// Otherwise just add sqrt( for new input
1329+
expression += 'sqrt(';
1330+
}
1331+
1332+
updateDisplay();
1333+
}
1334+
13171335
function appendFunction(func) {
13181336
if (justCalculated) {
13191337
expression = '';

0 commit comments

Comments
 (0)