calculator
const userInput = document.getElementById("userInput");
var expression = '';
function press(num){
expression += num;
userInput.value = expression;
}
function equal() {
userInput.value=eval(expression);
expression ='';
}
function erase(){
expression = '';
userInput.value=expression;
}
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>calculator</title>
<link rel="stylesheet" href="../css/style.css">
</head>
<body>
<div class="container">
<div class="calculator">
<input id="userInput" placeholder="0">
<br>
<button class="btn" onclick="press(1)">1</button>
<button class="btn" onclick="press(2)">2</button>
<button class="btn" onclick="press(3)">3</button>
<button class="btn" onclick="press('+')">+</button>
<br>
<button class="btn" onclick="press(4)">4</button>
<button class="btn" onclick="press(5)">5</button>
<button class="btn" onclick="press(6)">6</button>
<button class="btn" onclick="press('-')">-</button>
<br>
<button class="btn" onclick="press(7)">7</button>
<button class="btn" onclick="press(8)">8</button>
<button class="btn" onclick="press(9)">9</button>
<button class="btn" onclick="press('*')">x</button>
<br>
<button class="btn" onclick="press('.')">.</button>
<button class="btn" onclick="press(0)">0</button>
<button class="btn" onclick="erase()">C</button>
<button class="btn" onclick="press('/')">/</button>
<br>
<button class="btnEqual" onclick="equal()">=</button>
<br>
</div>
</div>
<script src="../javascript/script.js"></script>
</button>
</body>
</html>
css
----------------------------------------------------------
body{
background: linear-gradient(rgb(206, 206, 213),lightblue);
overflow: hidden;
}
.container{
width: 95%;
height: 100vh;
margin: auto;
display: flex;
justify-content: center;
align-items: center;
}
.calculator{
border: 1px solid black;
width: 370px;
margin: auto;
border-radius: 5px;
background-color: rgb(165, 230, 230);
padding: 20px;
box-shadow: 0 4px 2px 0;
}
#userInput{
box-sizing: border-box;
width: 100%;
padding: 20px 5px;
direction: ltr;
text-align: right;
}
.btn{
width: 24%;
height: 45px;
border: none;
background-color: antiquewhite;
margin-top: 2px;
outline:none ;
}
.btnEqual{
width: 99%;
height:45px ;
outline: none;
margin-top: 2px;
background-color: antiquewhite;
border: none;
}
.btn:hover, .btnEqual:hover{
background-color: rgb(201, 136, 51);
}
----------------------------------
js
const userInput = document.getElementById("userInput");
var expression = '';
function press(num){
expression += num;
userInput.value = expression;
}
function equal() {
userInput.value=eval(expression);
expression ='';
}
function erase(){
expression = '';
userInput.value=expression;
}
Comments
Post a Comment