Skip to content

Commit

Permalink
Fixed multiple operators in Basic Calculator (#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanu-agarwal0101 authored May 31, 2024
1 parent f6a9f90 commit 0f82ee6
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 14 deletions.
25 changes: 14 additions & 11 deletions Calculators/Basic-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,37 @@
<span class="num clear" onclick="calc.txt.value = '' "><i>C</i></span>
<span class="num" onclick="calc.txt.value += '(' "><i>(</i></span>
<span class="num" onclick="calc.txt.value += ')' "><i>)</i></span>
<span class="num" onclick="calc.txt.value += '/' "><i>/</i></span>

<span class="num" onclick="divide()"><i>/</i></span>

<span class="num clear-one cle" onclick="clearOne()"><i>CE</i></span>

<span class="num" onclick="calc.txt.value += '7' "><i>7</i></span>
<span class="num" onclick="calc.txt.value += '8' "><i>8</i></span>
<span class="num" onclick="calc.txt.value += '9' "><i>9</i></span>
<span class="num" onclick="calc.txt.value += '+' "><i>+</i></span>
<span class="num" onclick="calc.txt.value += '*' "><i>*</i></span>
<span class="num" onclick="calc.txt.value += '-' "><i>-</i></span>
<span class="num" onclick="add() "><i>+</i></span>
<span class="num" onclick="multiply() "><i>*</i></span>
<span class="num" onclick="subtract()"><i>-</i></span>
<span class="num" onclick="calc.txt.value += '4' "><i>4</i></span>
<span class="num" onclick="calc.txt.value += '5' "><i>5</i></span>
<span class="num" onclick="calc.txt.value += '6' "><i>6</i></span>
<span class="num" onclick="calc.txt.value += 'Math.sqrt(' "><i></i></span>
<span class="num" onclick="calc.txt.value += '%' "><i>%</i></span>
<span class="num" onclick="squareRoot()"><i></i></span>
<span class="num" onclick="modulus()"><i>%</i></span>
<span class="num point" onclick="point()"><i>.</i></span>
<span class="num" onclick="calc.txt.value += '1' "><i>1</i></span>
<span class="num" onclick="calc.txt.value += '2' "><i>2</i></span>
<span class="num" onclick="calc.txt.value += '3' "><i>3</i></span>
<span class="num" onclick="calc.txt.value += 'Math.sin(' "><i>sin</i></span>
<span class="num" onclick="calc.txt.value += 'Math.cos(' "><i>cos</i></span>
<span class="num" onclick="sine()"><i>sin</i></span>
<span class="num" onclick="cosine()"><i>cos</i></span>

<span class="num result-give equal cle"
onclick="document.calc.txt.value=eval(calc.txt.value)"><i>=</i></span>

<span class="num" onclick="calc.txt.value += ',' "><i>,</i></span>
<span class="num" onclick="calc.txt.value += '0' "><i>0</i></span>
<span class="num" onclick="calc.txt.value += 'Math.pow(' "><i>^</i></span>
<span class="num" onclick="calc.txt.value += 'Math.exp(' "><i>exp</i></span>
<span class="num" onclick="calc.txt.value += 'Math.log10(' "><i>log</i></span>
<span class="num" onclick="power()"><i>^</i></span>
<span class="num" onclick="exponent()"><i>exp</i></span>
<span class="num" onclick="logarithm()"><i>log</i></span>
</form>
</div>
<script src="script.js"></script>
Expand Down
126 changes: 123 additions & 3 deletions Calculators/Basic-Calculator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,22 @@ document.addEventListener("keydown", function (event) {
point();
}
else {
event.preventDefault();
document.getElementById("display").value += event.key;
if(event.key==="/" || event.key==="*" || event.key==="+" || event.key==="-" || event.key==="%" || event.key==="^"){
//prevent the default behaviour of event
event.preventDefault();
let currentValue = display.value;
let lastChar= currentValue.charAt(currentValue.length-1);

if(lastChar === "+" || lastChar === "-" || lastChar === "*" || lastChar === "/" || lastChar === "%" || lastChar === "^"){
//if last character is operator, remove it
currentValue=currentValue.substring(0, currentValue.length-1);
}
//append new operator
display.value = currentValue+event.key;
}else {
event.preventDefault();
document.getElementById("display").value += event.key;
}
}
} else if (event.key === "Enter") {
event.preventDefault();
Expand Down Expand Up @@ -166,4 +180,110 @@ function point() {
return;
}
}
};

};


function add() {
let currentValue = calc.txt.value;
// Check if the last character is an operator
let lastChar = currentValue.charAt(currentValue.length - 1);
if (lastChar === "+" || lastChar === "-" || lastChar === "*" || lastChar === "/" || lastChar === "%" || lastChar === "^") {
// If the last character is an operator, replace it with the new one
currentValue = currentValue.substring(0, currentValue.length - 1);
}
// Append the "+" operator
calc.txt.value = currentValue + "+";
}

function subtract() {
let currentValue = calc.txt.value;
let lastChar = currentValue.charAt(currentValue.length - 1);
if (lastChar === "+" || lastChar === "-" || lastChar === "*" || lastChar === "/" || lastChar === "%" || lastChar === "^") {
currentValue = currentValue.substring(0, currentValue.length - 1);
}
calc.txt.value = currentValue + "-";
}

function multiply() {
let currentValue = calc.txt.value;
let lastChar = currentValue.charAt(currentValue.length - 1);
if (lastChar === "+" || lastChar === "-" || lastChar === "*" || lastChar === "/" || lastChar === "%" || lastChar === "^") {
currentValue = currentValue.substring(0, currentValue.length - 1);
}
calc.txt.value = currentValue + "*";
}

function divide() {
let currentValue = calc.txt.value;
let lastChar = currentValue.charAt(currentValue.length - 1);
if (lastChar === "+" || lastChar === "-" || lastChar === "*" || lastChar === "/" || lastChar === "%" || lastChar === "^") {
currentValue = currentValue.substring(0, currentValue.length - 1);
}
calc.txt.value = currentValue + "/";
}

function modulus() {
let currentValue = calc.txt.value;
let lastChar = currentValue.charAt(currentValue.length - 1);
if (lastChar === "+" || lastChar === "-" || lastChar === "*" || lastChar === "/" || lastChar === "%" || lastChar === "^") {
currentValue = currentValue.substring(0, currentValue.length - 1);
}
calc.txt.value = currentValue + "%";
}

function power() {
let currentValue = calc.txt.value;
let lastChar = currentValue.charAt(currentValue.length - 1);
if (lastChar === "+" || lastChar === "-" || lastChar === "*" || lastChar === "/" || lastChar === "%" || lastChar === "^") {
currentValue = currentValue.substring(0, currentValue.length - 1);
}
calc.txt.value = currentValue + "^";
}

function logarithm() {
let currentValue = calc.txt.value;
let lastChar = currentValue.charAt(currentValue.length - 1);
if (lastChar === "+" || lastChar === "-" || lastChar === "*" || lastChar === "/" || lastChar === "%" || lastChar === "^") {
currentValue = currentValue.substring(0, currentValue.length - 1);
}
calc.txt.value = currentValue + "Math.log10(";
}

function exponent() {
let currentValue = calc.txt.value;
let lastChar = currentValue.charAt(currentValue.length - 1);
if (lastChar === "+" || lastChar === "-" || lastChar === "*" || lastChar === "/" || lastChar === "%" || lastChar === "^") {
currentValue = currentValue.substring(0, currentValue.length - 1);
}
calc.txt.value = currentValue + "Math.exp(";
}

function squareRoot() {
let currentValue = calc.txt.value;
let lastChar = currentValue.charAt(currentValue.length - 1);
if (lastChar === "+" || lastChar === "-" || lastChar === "*" || lastChar === "/" || lastChar === "%" || lastChar === "^") {
currentValue = currentValue.substring(0, currentValue.length - 1);
}
calc.txt.value = currentValue + "Math.sqrt(";
}

function sine() {
let currentValue = calc.txt.value;
let lastChar = currentValue.charAt(currentValue.length - 1);
if (lastChar === "+" || lastChar === "-" || lastChar === "*" || lastChar === "/" || lastChar === "%" || lastChar === "^") {
currentValue = currentValue.substring(0, currentValue.length - 1);
}
calc.txt.value = currentValue + "Math.sin(";
}

function cosine() {
let currentValue = calc.txt.value;
let lastChar = currentValue.charAt(currentValue.length - 1);
if (lastChar === "+" || lastChar === "-" || lastChar === "*" || lastChar === "/" || lastChar === "%" || lastChar === "^") {
currentValue = currentValue.substring(0, currentValue.length - 1);
}
calc.txt.value = currentValue + "Math.cos(";
}


0 comments on commit 0f82ee6

Please sign in to comment.