From 1f996ba8da2cbfc963febf56420c674bebdcc376 Mon Sep 17 00:00:00 2001 From: Minin <70429604+MininxD@users.noreply.github.com> Date: Sat, 20 Jan 2024 11:39:44 +0700 Subject: [PATCH] jumlah langsung saat di ketik serta fix deteksi () pada sin cos tan sin(10)cos(10) akan menjadi sin(10)*cos(10) --- index.html | 28 ++++++++++++++-------------- script.js | 28 ++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/index.html b/index.html index feef079..2c2a35c 100644 --- a/index.html +++ b/index.html @@ -14,8 +14,8 @@
π
-
(
-
)
+
(
+
)
sin
cos
tan
@@ -27,24 +27,24 @@
%
×
-
7
-
8
-
9
+
7
+
8
+
9
÷
-
4
-
5
-
6
+
4
+
5
+
6
-
-
1
-
2
-
3
+
1
+
2
+
3
+
-
.
-
0
-
del
+
.
+
0
+
del
=
diff --git a/script.js b/script.js index 3592552..5bb4273 100644 --- a/script.js +++ b/script.js @@ -1,5 +1,6 @@ var rumus = document.getElementById("rumusEl"); +var hasil = document.getElementById("hasilEl"); $('#satuEl').on("click", function() { $('#rumusEl').append("1"); @@ -98,13 +99,16 @@ $('#clearEl').on("click", function() { //hitung rumusnya -$('#jumlahEl').on("click", function() { - var numbers = [0,1,2,3,4,5,6,7,8,9]; - + +function hitungRumus() { var pi = rumus.textContent.replace(/π/g, "Math.PI"); // Fix perkalian () -var fix = pi.replace(/\)\(/g, ")*("); +var fixSin = pi.replace(/\)s/g, ")*s"); +var fixCos = fixSin.replace(/\)c/g, ")*c"); +var fixTan = fixCos.replace(/\)t/g, ")*t"); + +var fix = fixTan.replace(/\)\(/g, ")*("); var fix1 = fix.replace(/1\(/g, "1*("); var fix2 = fix1.replace(/2\(/g, "2*("); var fix3 = fix2.replace(/3\(/g, "3*("); @@ -148,12 +152,24 @@ var convertOperator = defineSqrt.replace(/\b(?:÷|×|\^|sin|cos|tan)\b/gi, func // console.log(convertOperator + "=" + eval(convertOperator)); - try { $('#hasilEl').html(eval(convertOperator)); $('#hasilEl').css("color", "black"); - } catch (e) { + } catch (error) { $('#hasilEl').html("Format Error"); $('#hasilEl').css("color", "red"); } +} + + +$('.btnNum').on("click", function() { + hitungRumus(); +}) +$('.btnTop').on("click", function() { + hitungRumus(); +}) +$('#jumlahEl').on("click", function() { + hitungRumus(); + rumus.innerHTML = hasil.textContent; + hasil.innerHTML =""; }) \ No newline at end of file