Skip to content

Commit

Permalink
jumlah langsung saat di ketik
Browse files Browse the repository at this point in the history
serta fix deteksi () pada sin cos tan

sin(10)cos(10) akan menjadi sin(10)*cos(10)
  • Loading branch information
mininxd authored Jan 20, 2024
1 parent 07d6934 commit 1f996ba
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
28 changes: 14 additions & 14 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<div class="btnChildTop">
<div id="piEl" class="btnTop pi">π</div>
<div id="sqrtEl" class="btnTop sqrt"></div>
<div id="bukaEl" class="btnTop tutup">(</div>
<div id="tutupEl" class="btnTop buka">)</div>
<div id="bukaEl" class="btnTop buka">(</div>
<div id="tutupEl" class="btnTop tutup">)</div>
<div id="sinEl" class="btnTop sin">sin</div>
<div id="cosEl" class="btnTop cos">cos</div>
<div id="tanEl" class="btnTop cos">tan</div>
Expand All @@ -27,24 +27,24 @@
<div id="percentEl" class="btn operator">%</div>
<div id="kaliEl" class="btn operator">×</div>
</div><div class="btnChild">
<div id="tujuhEl" class="btn">7</div>
<div id="delapanEl" class="btn">8</div>
<div id="sembilanEl" class="btn">9</div>
<div id="tujuhEl" class="btn btnNum">7</div>
<div id="delapanEl" class="btn btnNum">8</div>
<div id="sembilanEl" class="btn btnNum">9</div>
<div id="bagiEl" class="btn operator">÷</div>
</div><div class="btnChild">
<div id="empatEl" class="btn">4</div>
<div id="limaEl" class="btn">5</div>
<div id="enamEl" class="btn">6</div>
<div id="empatEl" class="btn btnNum">4</div>
<div id="limaEl" class="btn btnNum">5</div>
<div id="enamEl" class="btn btnNum">6</div>
<div id="kurangEl" class="btn operator">-</div>
</div><div class="btnChild">
<div id="satuEl" class="btn">1</div>
<div id="duaEl" class="btn">2</div>
<div id="tigaEl" class="btn">3</div>
<div id="satuEl" class="btn btnNum">1</div>
<div id="duaEl" class="btn btnNum">2</div>
<div id="tigaEl" class="btn btnNum">3</div>
<div id="tambahEl" class="btn operator">+</div>
</div><div class="btnChild">
<div id="titikEl" class="btn">.</div>
<div id="nolEl" class="btn">0</div>
<div id="deleteEl" class="btn">del</div>
<div id="titikEl" class="btn btnNum">.</div>
<div id="nolEl" class="btn btnNum">0</div>
<div id="deleteEl" class="btn btnNum">del</div>
<div id="jumlahEl" class="btn jumlah">=</div>
</div>
<p class="footer">mininxd</p>
Expand Down
28 changes: 22 additions & 6 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

var rumus = document.getElementById("rumusEl");
var hasil = document.getElementById("hasilEl");

$('#satuEl').on("click", function() {
$('#rumusEl').append("1");
Expand Down Expand Up @@ -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*(");
Expand Down Expand Up @@ -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 ="";
})

0 comments on commit 1f996ba

Please sign in to comment.