diff --git a/api/controller.js b/api/controller.js
index 3e03a4b..31bf8b8 100644
--- a/api/controller.js
+++ b/api/controller.js
@@ -26,6 +26,10 @@ exports.calculate = function (req, res) {
power: function (a, b) {
return Math.pow(a, b);
},
+ // natural logarithm (ln) - uses only operand1
+ ln: function (a) {
+ return Math.log(Number(a));
+ },
};
if (!req.query.operation) {
@@ -46,12 +50,15 @@ exports.calculate = function (req, res) {
throw new Error("Invalid operand1: " + req.query.operand1);
}
- if (
- !req.query.operand2 ||
- !req.query.operand2.match(/^(-)?[0-9\.]+(e(-)?[0-9]+)?$/) ||
- req.query.operand2.replace(/[-0-9e]/g, "").length > 1
- ) {
- throw new Error("Invalid operand2: " + req.query.operand2);
+ // For unary ops like ln we don't require operand2; for others we do
+ if (req.query.operation !== "ln") {
+ if (
+ !req.query.operand2 ||
+ !req.query.operand2.match(/^(-)?[0-9\.]+(e(-)?[0-9]+)?$/) ||
+ req.query.operand2.replace(/[-0-9e]/g, "").length > 1
+ ) {
+ throw new Error("Invalid operand2: " + req.query.operand2);
+ }
}
res.json({ result: operation(req.query.operand1, req.query.operand2) });
diff --git a/public/client.js b/public/client.js
index 9df4075..fc4db6a 100644
--- a/public/client.js
+++ b/public/client.js
@@ -35,6 +35,9 @@ function calculate(operand1, operand2, operation) {
case "^":
uri += "?operation=power";
break;
+ case "ln":
+ uri += "?operation=ln";
+ break;
default:
setError();
return;
@@ -113,8 +116,17 @@ function signPressed() {
}
function operationPressed(op) {
+ // store operand1
operand1 = getValue();
operation = op;
+
+ // ln is a unary operator (natural log) — compute immediately using operand1
+ if (op === "ln") {
+ state = states.complete;
+ calculate(operand1, 0, operation);
+ return;
+ }
+
state = states.operator;
}
diff --git a/public/index.html b/public/index.html
index 8fcfca5..5725dbb 100644
--- a/public/index.html
+++ b/public/index.html
@@ -43,6 +43,10 @@
+
+
+
+