Skip to content

Instantly share code, notes, and snippets.

@mattborn
Created February 25, 2016 04:38
Show Gist options
  • Select an option

  • Save mattborn/776d6f49dd7d8bdb8d55 to your computer and use it in GitHub Desktop.

Select an option

Save mattborn/776d6f49dd7d8bdb8d55 to your computer and use it in GitHub Desktop.
Vanilla JS Calculator
var buttons = document.querySelectorAll('.button');
var output = document.querySelector('.output');
var action = null;
var current = 0;
var actions = {
'±': ' - ',
'%': ' % ',
'÷': ' / ',
'×': ' * ',
'−': ' - ',
'+': ' + ',
};
for (button in buttons) {
buttons[button].onclick = function (e) {
var input = e.target.innerText;
var num = parseInt(input);
if (isNaN(num)) {
if (input === 'C') {
console.log('clear');
action = null;
current = 0;
output.innerText = 0;
} else {
if (action && action !== '=') {
var calculation = current + actions[action] + output.innerText;
console.log('calculate', calculation);
output.innerText = eval(calculation);
}
current = parseInt(output.innerText);
action = input;
}
} else {
if (current === parseInt(output.innerText)) {
output.innerText = num;
} else {
output.innerText += num;
}
}
console.log({
action: action,
current: current,
input: input,
});
};
}
document.addEventListener('keypress', function (e) {
if (e.which > 47 && e.which < 58) {
console.log('numbers!');
}
});
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Vanilla JS Calculator</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="calculator">
<div class="output">0</div>
<div class="button">C</div>
<div class="button">±</div>
<div class="button">%</div>
<div class="button accent">÷</div>
<div class="button">7</div>
<div class="button">8</div>
<div class="button">9</div>
<div class="button accent">×</div>
<div class="button">4</div>
<div class="button">5</div>
<div class="button">6</div>
<div class="button accent">−</div>
<div class="button">1</div>
<div class="button">2</div>
<div class="button">3</div>
<div class="button accent">+</div>
<div class="button double">0</div>
<div class="button">.</div>
<div class="button submit">=</div>
</div>
<script src="calculator.js"></script>
</body>
</html>
@import url(https://fonts.googleapis.com/css?family=Montserrat);
body {
background: #3e4149;
color: rgba(255,255,255,.2);
font: 200% Montserrat;
height: 100vh;
margin: 0;
text-align: center;
}
.calculator {
height: 100%;
margin: 0 auto;
max-width: 375px;
}
.output {
box-sizing: border-box;
color: #fd999a;
font-size: 48pt;
height: 25%;
padding: 10% 5% 5%;
text-align: right;
}
.button {
box-sizing: border-box;
cursor: pointer;
float: left;
height: 15%;
padding: 5%;
transition: .5s;
width: 25%;
}
.button:hover {
background: #444f59;
color: #fff;
}
.double {
width: 50%;
}
.accent:hover {
color: #fd999a;
}
.submit:hover {
background: #fd999a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment