Im making a calculator on JavaScript… but Im not able to think of any way to get a number, store it then after the operator is pressed get another number store it ….for n number of times
and then do the operation..
I have written some code, it just takes the first number and stores it .. please give me some idea to do operations here.
HTML
<div class="btns">
<button class="viewBox">here</button>
<button class="AC num">AC</button>
<button class="Ce num">CE</button>
<button class=" num blankBox"></button>
<button class="buttonide num">÷</button>
<button class="num7 num">7</button>
<button class="num8 num">8</button>
<button class="num9 num">9</button>
<button class="Multiply num">×</button>
<button class="num4 num">4</button>
<button class="num5 num">5</button>
<button class="num6 num">6</button>
<button class="Minus num">−</button>
<button class="num1 num">1</button>
<button class="num2 num">2</button>
<button class="num3 num">3</button>
<button class="Plus num">+</button>
<button class=" num blankBox"></button>
<button class="num0 num">0</button>
<button class=" num blankBox"> </button>
<button class="Equal num">=</button>
</div>
CSS
.calc{
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 100px;
}
.num{
/* -- HTML !-->
<button class="button-40" role="button">Button 40</button> */
background-color: #111827;
border: 1px solid transparent;
border-radius: .75rem;
box-sizing: border-box;
color: #FFFFFF;
cursor: pointer;
flex: 0 0 auto;
font-family: "Inter var",ui-sans-serif,system-ui,-apple-system,system-ui,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";
font-size: 1.125rem;
font-weight: 600;
line-height: 1.5rem;
padding: .75rem 1.2rem;
text-align: center;
text-decoration: none #6B7280 solid;
text-decoration-thickness: auto;
transition-duration: .2s;
transition-property: background-color,border-color,color,fill,stroke;
transition-timing-function: cubic-bezier(.4, 0, 0.2, 1);
user-select: none;
-webkit-user-select: none;
touch-action: manipulation;
width: auto;
}
.num:hover {
background-color: #374151;
}
.num:focus {
box-shadow: none;
outline: 2px solid transparent;
outline-offset: 2px;
}
@media (min-width: 768px) {
.num {
padding: .75rem 1.5rem;
}
}
.btns{
font-size: 2rem;
display:grid;
grid-template-columns: repeat(4, 120px);
grid-auto-rows: 120px;
}
.viewBox {
background-color: #111827;
border: 1px solid transparent;
border-radius: .75rem;
box-sizing: border-box;
text-align: right;
color: #FFFFFF;
cursor: pointer;
flex: 0 0 auto;
font-family: "Inter var",ui-sans-serif,system-ui,-apple-system,system-ui,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";
font-size: 2rem;
font-weight: 600;
line-height: 1.5rem;
text-align: center;
text-decoration: none #6B7280 solid;
text-decoration-thickness: auto;
transition-duration: .2s;
transition-property: background-color,border-color,color,fill,stroke;
transition-timing-function: cubic-bezier(.4, 0, 0.2, 1);
user-select: none;
-webkit-user-select: none;
touch-action: manipulation;
width: auto;
display: flex;
align-items: center;
grid-area: header;
text-align: center;
border: 1px solid black;
grid-column-start: 1;
grid-column-end: 5;
grid-row-start: 1;
grid-row-end: 2;
;
}
.num{
display: flex;
align-items: center;
justify-content: center ;
line-height: 1.6;
font-size: 1.5rem;
border: 1px solid black;
}
JavaScript
let runningTotal = 0;
let start = "";
const screen = document.querySelector(".viewBox");
document
.querySelector(".btns")
.addEventListener("click", function(event) {
buttonClick(event.target.innerText) ;
});
function buttonClick(value) {
if (isNaN(parseInt(value))) {
symbolhandle(value) ;
} else {
numberhandle(value);
}
}
function symbolhandle(value){
if (value = "+") {
runningTotal =+ screen.innerText;
screen.innerText = null ;
console.log(runningTotal);
}
}
function numberhandle(value){
if (start === 0 ) {
start = value;
} else {
start += value ;
} rerender();
}
function rerender(value){
screen.innerText = start;
runningTotal = start;
}
document
.querySelector(".AC.num")
.addEventListener("click", function(value){
start = "";
rerender();
})