Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js file and index.html changed #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/Simple-Calculator.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 34 additions & 34 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,41 @@

<body>

<table class="calculator">
<tr>
<td colspan="3"><input class="display-box" type="text" id="result" disabled /></td>
<!-- clearScreen() function clear all the values -->
<td><input class="button" type="button" value="C" onclick="clearScreen()" style="background-color: #fb0066;" /> </td>
</tr>
<tr>
<!-- display() function display the value of clicked button -->
<td><input class="button" type="button" value="1" onclick="display('1')" /> </td>
<td><input class="button" type="button" value="2" onclick="display('2')" /> </td>
<td><input class="button" type="button" value="3" onclick="display('3')" /> </td>
<td><input class="button" type="button" value="/" onclick="display('/')" /> </td>
</tr>
<tr>
<td><input class="button" type="button" value="4" onclick="display('4')" /> </td>
<td><input class="button" type="button" value="5" onclick="display('5')" /> </td>
<td><input class="button" type="button" value="6" onclick="display('6')" /> </td>
<td><input class="button" type="button" value="-" onclick="display('-')" /> </td>
</tr>
<tr>
<td><input class="button" type="button" value="7" onclick="display('7')" /> </td>
<td><input class="button" type="button" value="8" onclick="display('8')" /> </td>
<td><input class="button" type="button" value="9" onclick="display('9')" /> </td>
<td><input class="button" type="button" value="+" onclick="display('+')" /> </td>
</tr>
<tr>
<td><input class="button" type="button" value="." onclick="display('.')" /> </td>
<td><input class="button" type="button" value="0" onclick="display('0')" /> </td>
<!-- calculate() function evaluate the mathematical expression -->
<td><input class="button" type="button" value="=" onclick="calculate()" style="background-color: #fb0066;" /> </td>
<td><input class="button" type="button" value="*" onclick="display('*')" /> </td>
</tr>
</table>
<table class="calculator">
<tr>
<td colspan="3"><input class="display-box" type="text" id="result" disabled /></td>
<!-- clearScreen() function clear all the values -->
<td><input class="button" type="button" value="C" onclick="clearScreen()" style="background-color: #fb0066;" /> </td>
</tr>
<tr>
<!-- display() function display the value of clicked button -->
<td><input class="button btn" type="button" value="1" /> </td>
<td><input class="button btn" type="button" value="2" /> </td>
<td><input class="button btn" type="button" value="3" /> </td>
<td><input class="button btn" type="button" value="/" /> </td>
</tr>
<tr>
<td><input class="button btn" type="button" value="4" /> </td>
<td><input class="button btn" type="button" value="5" /> </td>
<td><input class="button btn" type="button" value="6" /> </td>
<td><input class="button btn" type="button" value="-" /> </td>
</tr>
<tr>
<td><input class="button btn" type="button" value="7" /> </td>
<td><input class="button btn" type="button" value="8" /> </td>
<td><input class="button btn" type="button" value="9" /> </td>
<td><input class="button btn" type="button" value="+" /> </td>
</tr>
<tr>
<td><input class="button btn" type="button" value="." /> </td>
<td><input class="button btn" type="button" value="0" /> </td>
<!-- calculate() function evaluate the mathematical expression -->
<td><input class="button" type="button" value="=" onclick="calculate()" style="background-color: #fb0066;" /> </td>
<td><input class="button btn" type="button" value="*" /> </td>
</tr>
</table>

<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>

</html>
28 changes: 24 additions & 4 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
let result = document.getElementById("result")
let btn = document.querySelectorAll('.btn')
btn.forEach(function (btn) {
btn.addEventListener('click', function () {
display(btn.value)
})
})


// This function clear all the values
function clearScreen() {
document.getElementById("result").value = "";
result.value = "";
}


// This function display values
function display(value) {
document.getElementById("result").value += value;
if (value=='+' ||value=='-' || value=='*'||value=='/'){
if (result.value[result.value.length-1] == '+' ||
result.value[result.value.length-1] == '-' ||
result.value[result.value.length-1] == '*' ||
result.value[result.value.length-1] == '/' ){

} else {result.value += value;}
} else {

result.value += value;

}
}

// This function evaluates the expression and return result
function calculate() {
var p = document.getElementById("result").value;
var p = result.value;
var q = eval(p);
document.getElementById("result").value = q;
result.value = q;
}