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

Simple Calculator JS #510

Open
wants to merge 3 commits 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
7 changes: 7 additions & 0 deletions simple calculator/bootstrap.min.css

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions simple calculator/calculator.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
background-color: #0b0f13;
}

.screen {
height: 70px;
background-color: #151319;
margin-bottom: 9px;
border: 2px solid #e44d08;
border-radius: 5px;
}

table {
margin-top: 60px;
}

td {
/* display: inline-block;*/
}

.key {
box-shadow: border-box;
height: 60px;
font-size: 30px;
color: white;
border: 1px solid #e44d08;
border-radius: 10px

}

td.key:hover {
background-color: #bdb60f;
font-size: 23px;
box-shadow: 0 0 23px 1px gold;
}

#text {
font-size: 20px;
color: white;
}

#answer {
font-size: 22px;
color: white;
}
44 changes: 44 additions & 0 deletions simple calculator/calculator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel='stylesheet' type="text/css" href='bootstrap.min.css'>
<link rel='stylesheet' type="text/css" href='calculator.css'>
</head>
<body class="container w-25">
<h1 class="header text-center" style="color: aqua; margin-top:20px;2">Simple Calculator</h1>
<table class="text-center w-100"
<tr>
<td colspan="4" class="form-control screen" id="screen"><span id="text"></span> <br> <span id="answer"></span></td>
</tr>
<tr>
<td class="btn key w-25" id="one">1</td>
<td class="btn key w-25" id="two">2</td>
<td class="btn key w-25" id="three">3</td>
<td class="btn key w-25" id="add">+</td>
</tr>
<tr>
<td class="btn key w-25" id="four">4</td>
<td class="btn key w-25" id="five">5</td>
<td class="btn key w-25" id="six">6</td>
<td class="btn key w-25" id="subtract">-</td>
</tr>
<tr>
<td class="btn key w-25" id="seven">7</td>
<td class="btn key w-25" id="eight">8</td>
<td class="btn key w-25" id="nine">9</td>
<td class="btn key w-25" id="mlpn">*</td>
</tr>
<tr>
<td class="btn key w-25" id="divide">/</td>
<td class="btn key w-25" id="zero">0</td>
<td class="btn key w-25" id="equal">=</td>
<td class="btn key w-25" id="clean">C</td>
</tr>

</table>


<script src="calculator.js"></script>
</body>
</html>
91 changes: 91 additions & 0 deletions simple calculator/calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// getting elements by dom:
let zero = document.querySelector("#zero")
let one = document.querySelector("#one")
let two = document.querySelector("#two")
let three = document.querySelector("#three")
let four = document.querySelector("#four")
let five = document.querySelector("#five")
let six = document.querySelector("#six")
let seven = document.querySelector("#seven")
let eight = document.querySelector("#eight")
let nine = document.querySelector("#nine")
let add = document.querySelector("#add")
let subtract = document.querySelector("#subtract")
let mlpn = document.querySelector("#mlpn")
let divide = document.querySelector("#divide")
let equal = document.querySelector("#equal")
let clean = document.querySelector("#clean")
let text = document.querySelector("#text")
let answer = document.querySelector("#answer")

// adding events to the calculator:
zero.addEventListener("click",e => {
text.textContent += 0
})

one.addEventListener("click",e => {
text.textContent += 1
})

two.addEventListener("click",e => {
text.textContent += 2
})

three.addEventListener("click",e => {
text.textContent += 3
})

four.addEventListener("click",e => {
text.textContent += 4
})

five.addEventListener("click",e => {
text.textContent += 5
})

six.addEventListener("click",e => {
text.textContent += 6
})

seven.addEventListener("click",e => {
text.textContent += 7
})

eight.addEventListener("click",e => {
text.textContent += 8
})

nine.addEventListener("click",e => {
text.textContent += 9
})

add.addEventListener("click",e => {
text.textContent += '+'

})

subtract.addEventListener("click",e => {
text.textContent += '-'
})

mlpn.addEventListener("click",e => {
text.textContent += '*'
})

divide.addEventListener("click",e => {
text.textContent += '/'
})

clean.addEventListener("click",e => {
text.textContent= ''
answer.textContent= ''
})

equal.addEventListener("click",e => {
answer.textContent = `Answer: ${eval(text.textContent)}`
text.textContent=''


})