Skip to content

Commit

Permalink
fixed DOM issues
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos committed Nov 14, 2023
1 parent b49b30d commit 0970cb8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
24 changes: 13 additions & 11 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
<!-- starting with the html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>TOPCalculator</title>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
<title>Document</title>
</head>
<body>
<main id="main">
<form action="" id="calc_form">
<form id="calc_form">
<header class="calc_header">
<input
type="text"
disabled
id="output"
class="calc_input"
class="calc_output"
value="0"
/>
</header>
Expand All @@ -28,12 +26,15 @@
value="clear"
class="btn btn-grey"
>
AC</button
><button data-type="operator" value="invert" class="btn btn-grey">
+/-</button
><button data-type="operator" value="%" class="btn btn-grey">
%</button
><button data-type="operator" value="/" class="btn btn-grey">
AC
</button>
<button data-type="operator" value="invert" class="btn btn-grey">
+/-
</button>
<button data-type="operator" value="%" class="btn btn-grey">
%
</button>
<button data-type="operator" value="/" class="btn btn-orange">
÷
</button>
</div>
Expand Down Expand Up @@ -103,4 +104,5 @@
</form>
</main>
</body>
<script src="script.js"></script>
</html>
46 changes: 42 additions & 4 deletions script.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
}
});
});

0 comments on commit 0970cb8

Please sign in to comment.