From a14b97e7e59784c635658289619f9a1b8f6af041 Mon Sep 17 00:00:00 2001
From: Samar1110 <98534735+Samar1110@users.noreply.github.com>
Date: Thu, 9 Jun 2022 23:51:20 +0530
Subject: [PATCH 1/8] Add files via upload
---
index.html | 97 ++++++++++++++++++++++++++++++
script.js | 158 +++++++++++++++++++++++++++++++++++++++++++++++++
storingData.js | 82 +++++++++++++++++++++++++
style.css | 130 ++++++++++++++++++++++++++++++++++++++++
4 files changed, 467 insertions(+)
create mode 100644 index.html
create mode 100644 script.js
create mode 100644 storingData.js
create mode 100644 style.css
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..b466de0
--- /dev/null
+++ b/index.html
@@ -0,0 +1,97 @@
+
+
+
+
+
+
+ Web Calculator
+
+
+
+
+
+
+
+
+
+
+
+ 0
+
+
+
+
sin^-1
+
cos^-1
+
tan^-1
+
e
+
+
+
+
+
+
+
+
+
+
+
+
History
+
+
+
+ | First Num |
+ Operator |
+ Second Num |
+ Result |
+
+
+
+
+
+
+
+
Copyright 2022 Phantom
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/script.js b/script.js
new file mode 100644
index 0000000..47ef9ca
--- /dev/null
+++ b/script.js
@@ -0,0 +1,158 @@
+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 === "^") {
+ result = Math.pow(parseFloat(calculator.firstNumber),parseFloat(calculator.displayNumber) )
+ }
+ if (calculator.operator === "sqrt") {
+ result = Math.sqrt(parseFloat(calculator.firstNumber))
+ }
+ if (calculator.operator == "x^2") {
+ result = Math.pow(parseFloat(calculator.firstNumber),2)
+ }
+ if (calculator.operator == "|x|") {
+ result = Math.abs(parseFloat(calculator.firstNumber))
+ }
+ if (calculator.operator == "sin^-1") {
+ result = Math.asin(parseFloat(calculator.firstNumber))
+ }
+ if (calculator.operator == "cos^-1") {
+ result = Math.acos(parseFloat(calculator.firstNumber))
+ }
+ if (calculator.operator == "tan^-1") {
+ result = Math.atan(parseFloat(calculator.firstNumber))
+ }
+ if (calculator.operator == "e") {
+ result = Math.exp(parseFloat(calculator.firstNumber))
+ }
+ // if()
+
+
+ // 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();
+}
+
+const buttons = document.querySelectorAll(".button");
+for (let button of buttons) {
+ button.addEventListener('click', function (event) {
+
+ // mendapatkan objek elemen yang diklik
+ 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/storingData.js b/storingData.js
new file mode 100644
index 0000000..bcc7889
--- /dev/null
+++ b/storingData.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..177bbe7
--- /dev/null
+++ b/style.css
@@ -0,0 +1,130 @@
+* {
+ margin: 0;
+ box-sizing: border-box;
+}
+
+body {
+ /* background-image: url("download (10).jpg"); */
+ font-family: sans-serif;
+}
+
+.heading{
+ background-color: #018558;
+ color: #bde902;
+ font-size: 5rem;
+ text-align: center;
+ margin-bottom: 25px;
+ font-family: 'Roboto Slab', serif;
+}
+
+.flex-container-column {
+ display: flex;
+ flex-direction: column;
+ background-color: #e9edc9;
+ 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-color: #bde902;
+ cursor: pointer;
+}
+
+.double {
+ flex-basis: 50%;
+}
+
+.display {
+ color: #bde902;
+ width: 100%;
+ padding: 10px 20px;
+ background-color: #333333;
+ border: 1px solid black;
+ font-size: 2em;
+}
+
+.operator,
+.negative,
+.equals {
+ background: #018558;
+ color: #bde902;
+}
+
+.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;
+}
+
+table {
+ border-collapse: collapse;
+ border-spacing: 0;
+ width: 100%;
+ border: 1px solid #ddd;
+}
+
+th,
+td {
+ text-align: center;
+ padding: 16px;
+}
+
+th {
+ background-color: #018558;
+
+ color: #bde902;
+}
+
+tr:nth-child(even) {
+ background-color: white;
+}
+
+.center {
+ margin-top: 50px;
+ text-align: center;
+ font-size: 5rem;
+ margin: auto;
+ font-family: 'Roboto Slab', serif;
+ background-color: #018558;
+ color: #bde902;
+ padding: 10px;
+ }
+
+@media screen and (max-width: 513px) {
+ .button {
+ padding: 10px;
+ }
+
+ .history {
+ width: 100%;
+ }
+}
\ No newline at end of file
From 088f42ff26c1855372ae80a9bde79cbd7c86615c Mon Sep 17 00:00:00 2001
From: Samar1110 <98534735+Samar1110@users.noreply.github.com>
Date: Tue, 5 Jul 2022 01:29:46 +0530
Subject: [PATCH 2/8] Update index.html
---
index.html | 69 +++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 47 insertions(+), 22 deletions(-)
diff --git a/index.html b/index.html
index b466de0..e4fe1b4 100644
--- a/index.html
+++ b/index.html
@@ -7,43 +7,68 @@
Web Calculator
+
+
-
+
-
Calculator
+
Calculator
+
+
+
-
+
0
-
sin^-1
-
cos^-1
-
tan^-1
-
e
+
sin^-1
+
cos^-1
+
tan^-1
+
e
-
|x|
-
x^2
-
sqrt
-
/
+
|x|
+
x^2
+
sqrt
+
ln
+
-
ln
-
log
-
1/x
-
*
+
+
log
+
sin
+
cos
+
tan
-
sin
-
cos
-
tan
-
^
+
%
+
/
+
*
+
CE
7
@@ -89,9 +114,9 @@
History
-
+
-
+
-