From 247a345466ed247bf011d28559ef41b21f8746b4 Mon Sep 17 00:00:00 2001
From: AnantJain05 <74446727+AnantJain05@users.noreply.github.com>
Date: Thu, 9 Jun 2022 14:16:44 -0400
Subject: [PATCH 1/7] Add files via upload
---
app.js | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++
index.html | 92 ++++++++++++++++++++++++++++++++++
storage.js | 82 +++++++++++++++++++++++++++++++
style.css | 131 ++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 447 insertions(+)
create mode 100644 app.js
create mode 100644 index.html
create mode 100644 storage.js
create mode 100644 style.css
diff --git a/app.js b/app.js
new file mode 100644
index 0000000..246dd1d
--- /dev/null
+++ b/app.js
@@ -0,0 +1,142 @@
+const calculator = {
+ displayNumber: '0',
+ operator: null,
+ firstNumber: null,
+ waitingForSecondNumber: false
+};
+
+function updateDisplay() {
+ document.querySelector("#displayNumber").innerText = calculator.displayNumber;
+}
+
+function clearCalculator() {
+ calculator.displayNumber = '0';
+ calculator.operator = null;
+ calculator.firstNumber = null;
+ calculator.waitingForSecondNumber = false;
+}
+
+function inputDigit(digit) {
+ if (calculator.waitingForSecondNumber && calculator.firstNumber === calculator.displayNumber) {
+ calculator.displayNumber = digit;
+ } else {
+ if (calculator.displayNumber === '0') {
+ calculator.displayNumber = digit;
+ } else {
+ calculator.displayNumber += digit;
+ }
+ }
+}
+
+function inverseNumber() {
+ if (calculator.displayNumber === '0') {
+ return;
+ }
+ calculator.displayNumber = calculator.displayNumber * -1;
+}
+
+function handleOperator(operator) {
+ if (!calculator.waitingForSecondNumber) {
+ calculator.operator = operator;
+ calculator.waitingForSecondNumber = true;
+ calculator.firstNumber = calculator.displayNumber;
+ } else {
+ alert('Enter 2nd number first')
+ }
+}
+
+function performCalculation() {
+ if (calculator.firstNumber == null || calculator.operator == null) {
+ alert("Enter a number first");
+ return;
+ }
+
+ let result = 0;
+ if (calculator.operator === "+") {
+ result = parseFloat(calculator.firstNumber) + parseFloat(calculator.displayNumber);
+ }
+ if (calculator.operator === "-") {
+ result = parseFloat(calculator.firstNumber) - parseFloat(calculator.displayNumber)
+ }
+ if (calculator.operator === "*") {
+ result = parseFloat(calculator.firstNumber) * parseFloat(calculator.displayNumber)
+ }
+ if (calculator.operator === "/") {
+ result = parseFloat(calculator.firstNumber) / parseFloat(calculator.displayNumber)
+ }
+ if (calculator.operator === "1/x") {
+ result = 1/parseFloat(calculator.firstNumber)
+ }
+ if (calculator.operator === "sin") {
+ result = Math.sin(parseFloat(calculator.firstNumber))
+ }
+ if (calculator.operator === "cos") {
+ result = Math.cos(parseFloat(calculator.firstNumber))
+ }
+ if (calculator.operator === "tan") {
+ result = Math.tan(parseFloat(calculator.firstNumber))
+ }
+ if (calculator.operator === "log") {
+ result = Math.log10(parseFloat(calculator.firstNumber))
+ }
+ if (calculator.operator === "ln") {
+ result = Math.log(parseFloat(calculator.firstNumber))
+ }
+ if (calculator.operator === "sqrt") {
+ result = Math.sqrt(parseFloat(calculator.firstNumber))
+ }
+ if (calculator.operator == "π") {
+ result = Math.PI * (parseFloat(calculator.firstNumber))
+ }
+ if (calculator.operator == "e") {
+ result = Math.exp(parseFloat(calculator.firstNumber))
+ }
+ if (calculator.operator == "|x|") {
+ result = Math.abs(parseFloat(calculator.firstNumber))
+ }
+
+ const history = {
+ firstNumber: calculator.firstNumber,
+ secondNumber: calculator.displayNumber,
+ operator: calculator.operator,
+ result: result
+ }
+ putHistory(history);
+ calculator.displayNumber = result;
+ renderHistory();
+}
+
+const buttons = document.querySelectorAll(".button");
+for (let button of buttons) {
+ button.addEventListener('click', function (event) {
+
+ const target = event.target;
+
+ if (target.classList.contains('clear')) {
+ clearCalculator();
+ updateDisplay();
+ return;
+ }
+
+ if (target.classList.contains('negative')) {
+ inverseNumber();
+ updateDisplay();
+ return;
+ }
+
+ if (target.classList.contains('equals')) {
+ performCalculation();
+ updateDisplay();
+ return;
+ }
+
+ if (target.classList.contains('operator')) {
+ handleOperator(target.innerText)
+ updateDisplay();
+ return;
+ }
+
+ inputDigit(target.innerText);
+ updateDisplay()
+ });
+}
\ No newline at end of file
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..57af848
--- /dev/null
+++ b/index.html
@@ -0,0 +1,92 @@
+
+
+
+
+
+
+ Web Calculator
+
+
+
+
+
+
+
+
+
+
+
+ 0
+
+
+
+
+
+
+
+
+
+
+
+
+
History
+
+
+
+ Operand |
+ Operator |
+ Operand |
+ Result |
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/storage.js b/storage.js
new file mode 100644
index 0000000..bcc7889
--- /dev/null
+++ b/storage.js
@@ -0,0 +1,82 @@
+const CACHE_KEY = "calculation_history";
+
+function checkForStorage() {
+ return typeof (Storage) !== "undefined";
+}
+
+function putHistory(data) {
+ if (checkForStorage()) {
+ let historyData = null;
+ if (localStorage.getItem(CACHE_KEY) === null) {
+ historyData = [];
+ } else {
+ historyData = JSON.parse(localStorage.getItem(CACHE_KEY));
+ }
+
+ historyData.unshift(data);
+
+ if (historyData.length > 5) {
+ historyData.pop();
+ }
+
+ localStorage.setItem(CACHE_KEY, JSON.stringify(historyData));
+ }
+}
+
+function showHistory() {
+ if (checkForStorage) {
+ return JSON.parse(localStorage.getItem(CACHE_KEY)) || [];
+ } else {
+ return [];
+ }
+}
+
+function renderHistory() {
+ const historyData = showHistory();
+ let historyList = document.querySelector("#historyList");
+ historyList.innerHTML = "";
+
+ for (let history of historyData) {
+ let row = document.createElement('tr');
+ row.innerHTML = "" + history.firstNumber + " | ";
+ row.innerHTML += "" + history.operator + " | ";
+ row.innerHTML += "" + history.secondNumber + " | ";
+ row.innerHTML += "" + history.result + " | ";
+
+ historyList.appendChild(row);
+ }
+}
+
+renderHistory();
+
+const history = {
+ firstNumber: calculator.firstNumber,
+ secondNumber: calculator.displayNumber,
+ operator: calculator.operator,
+ result: result
+}
+
+function performCalculation() {
+ if (calculator.firstNumber == null || calculator.operator == null) {
+ alert("Anda belum menetapkan operator");
+ return;
+ }
+
+ let result = 0;
+ if (calculator.operator === "+") {
+ result = parseInt(calculator.firstNumber) + parseInt(calculator.displayNumber);
+ } else {
+ result = parseInt(calculator.firstNumber) - parseInt(calculator.displayNumber)
+ }
+
+ // objek yang akan dikirimkan sebagai argumen fungsi putHistory()
+ const history = {
+ firstNumber: calculator.firstNumber,
+ secondNumber: calculator.displayNumber,
+ operator: calculator.operator,
+ result: result
+ }
+ putHistory(history);
+ calculator.displayNumber = result;
+ renderHistory();
+}
\ No newline at end of file
diff --git a/style.css b/style.css
new file mode 100644
index 0000000..d41a727
--- /dev/null
+++ b/style.css
@@ -0,0 +1,131 @@
+* {
+ box-sizing: border-box;
+}
+
+body {
+ font-family: sans-serif;
+ margin: 0;
+ background-color: #E9D5CA;
+}
+
+.heading{
+ background-color: #827397;
+ color: #363062;
+ font-size: 4rem;
+ font-weight: bold;
+ text-align: center;
+ font-family: 'Libre Baskerville', serif;
+ margin: 0;
+ padding: 10px;
+}
+
+.flex-container-column {
+ display: flex;
+ flex-direction: column;
+ background-color: #363062;
+ max-width: 800px;
+ margin: auto;
+ text-align: right;
+}
+
+.flex-container-row {
+ display: flex;
+}
+
+.button {
+ flex-basis: 25%;
+
+ font-size: 1.5em;
+ text-align: center;
+ padding: 40px;
+ border: 1px solid black;
+ /* background: -webkit-linear-gradient(top, #d2d2d2, #ddd); */
+ background-color: #E9D5CA;
+ cursor: pointer;
+}
+
+.double {
+ flex-basis: 50%;
+}
+
+.display {
+ color: #4D4C7D;
+ width: 100%;
+ padding: 10px 20px;
+ background-color: white;
+ border: 1px solid black;
+ font-size: 2em;
+}
+
+.operator,
+.negative,
+.equals {
+ background: #827397;
+}
+
+.card {
+ box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
+ border-radius: 5px;
+ padding: 30px;
+ margin-top: 20px;
+}
+
+.button:hover {
+ font-weight: bold;
+}
+
+@media screen and (max-width:513px) {
+ .button {
+ padding: 10px;
+ margin: auto;
+ }
+}
+
+.history {
+ width: 80%;
+ margin: 30px auto 0 auto;
+ overflow: scroll;
+ background-color: white;
+ text-align: center;
+}
+
+table {
+ border-collapse: collapse;
+ border-spacing: 0;
+ width: 100%;
+ border: 1px solid #ddd;
+}
+
+th,
+td {
+ text-align: center;
+ padding: 16px;
+}
+
+th {
+ background-color: #363062;
+ color: white;
+}
+
+tr:nth-child(even) {
+ background-color: #827397;
+}
+
+.center {
+ margin-top: 50px;
+ text-align: center;
+ font-size: 3rem;
+ margin: auto;
+ background-color: orange;
+ padding: 10px;
+ }
+
+@media screen and (max-width: 513px) {
+ .button {
+ padding: 10px;
+ }
+
+ .history {
+ width: 100%;
+ }
+}
\ No newline at end of file
From 1dd0d56212230544f0f3f91468acb89e29ecc753 Mon Sep 17 00:00:00 2001
From: AnantJain05 <74446727+AnantJain05@users.noreply.github.com>
Date: Tue, 5 Jul 2022 01:28:26 +0530
Subject: [PATCH 2/7] Update index.html
---
index.html | 169 +++++++++++++++++++++++++++++++++--------------------
1 file changed, 105 insertions(+), 64 deletions(-)
diff --git a/index.html b/index.html
index 57af848..3e2a14a 100644
--- a/index.html
+++ b/index.html
@@ -3,90 +3,131 @@
-
+
Web Calculator
-
+
+
+
+
+
+
-
-
-
-
- 0
-
-
-
-
-
-
-
-
4
-
5
-
6
-
-
+