diff --git a/index.html b/index.html
index bb7313b..b34ff57 100644
--- a/index.html
+++ b/index.html
@@ -1,22 +1,20 @@
-
- TOPCalculator
-
+ Document
-
+
diff --git a/script.js b/script.js
index 6ef449b..7b8c750 100644
--- a/script.js
+++ b/script.js
@@ -1,18 +1,24 @@
-console.log("this is working");
const output = document.getElementById("output");
const form = document.getElementById("calc_form");
const operand_btns = document.querySelectorAll("button[data-type=operand]");
+const operator_btns = document.querySelectorAll("button[data-type=operator]");
form.addEventListener("submit", (e) => {
e.preventDefault();
});
-console.log(output.value);
-console.log(form.value);
-console.log(operand_btns.values);
let is_operator = false;
+let equation = [];
+
+const remove_active = () => {
+ operator_btns.forEach((btn) => {
+ btn.classList.remove("active");
+ });
+};
+
operand_btns.forEach((btn) => {
btn.addEventListener("click", (e) => {
+ remove_active();
if (output.value == "0") {
output.value = e.target.value;
} else if (is_operator) {
@@ -25,3 +31,35 @@ operand_btns.forEach((btn) => {
}
});
});
+
+operator_btns.forEach((btn) => {
+ btn.addEventListener("click", (e) => {
+ remove_active();
+ e.currentTarget.classList.add("active");
+
+ switch (e.target.value) {
+ case "%":
+ output.value = parseFloat(output.value) / 100;
+ break;
+ case "invert":
+ output.value = parseFloat(output.value) * -1;
+ break;
+ case "=":
+ equation.push(output.value);
+ output.value = eval(equation.join(""));
+ equation = [];
+ break;
+ default:
+ let last_item = equation[equation.length - 1];
+ if (["/", "*", "+", "-"].includes(last_item) && is_operator) {
+ equation.pop();
+ equation.push(e.target.value);
+ } else {
+ equation.push(output.value);
+ equation.push(e.target.value);
+ }
+ is_operator = true;
+ break;
+ }
+ });
+});