o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
create a working calculator program with just html css js'
- User
create a working calculator program with just html css js'
- Assistant
create a working calculator program with just html css js'
Sure! Below is a simple calculator program built using only HTML, CSS, and JavaScript. This calculator can perform basic arithmetic operations: addition, subtraction, multiplication, and division.
index.html):<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Calculator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="calculator">
<input type="text" class="calculator-screen" value="" disabled />
<div class="calculator-keys">
<button type="button" class="operator" value="+">+</button>
<button type="button" class="operator" value="-">−</button>
<button type="button" class="operator" value="*">×</button>
<button type="button" class="operator" value="/">÷</button>
<button type="button" value="7">7</button>
<button type="button" value="8">8</button>
<button type="button" value="9">9</button>
<button type="button" value="4">4</button>
<button type="button" value="5">5</button>
<button type="button" value="6">6</button>
<button type="button" value="1">1</button>
<button type="button" value="2">2</button>
<button type="button" value="3">3</button>
<button type="button" value="0">0</button>
<button type="button" class="decimal" value=".">.</button>
<button type="button" class="all-clear" value="all-clear">AC</button>
<button type="button" class="equal-sign operator" value="=">=</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
styles.css):/* General Styles */
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f3f3f3;
}
.calculator {
width: 400px;
margin: 100px auto;
border: 1px solid #ccc;
border-radius: 10px;
overflow: hidden;
}
/* Screen Styles */
.calculator-screen {
width: 100%;
height: 80px;
border: none;
background-color: #252525;
color: #fff;
text-align: right;
font-size: 4rem;
padding-right: 20px;
padding-left: 10px;
}
/* Button Styles */
.calculator-keys {
display: grid;
grid-template-columns: repeat(4, 1fr);
}
button {
height: 80px;
font-size: 2rem;
border: 1px solid #ddd;
background-color: #e0e0e0;
color: #333;
cursor: pointer;
}
button:hover {
background-color: #d0d0d0;
}
.operator {
background-color: #ff9500;
color: #fff;
}
.all-clear {
background-color: #ff3b30;
color: #fff;
}
.equal-sign {
grid-column: span 4;
background-color: #007aff;
color: #fff;
}
script.js):// Get all the keys from the document
const keys = document.querySelector('.calculator-keys');
const screen = document.querySelector('.calculator-screen');
let currentInput = '0';
let previousInput = '';
let operator = null;
// Add event listener to the keys
keys.addEventListener('click', event => {
const { target } = event;
const { value } = target;
// If the clicked element is not a button, exit function
if (!target.matches('button')) {
return;
}
switch (value) {
case '+':
case '-':
case '*':
case '/':
handleOperator(value);
break;
case '=':
calculate();
break;
case 'all-clear':
resetCalculator();
break;
case '.':
inputDecimal(value);
break;
default:
if (Number.isInteger(parseFloat(value))) {
inputNumber(value);
}
}
updateScreen();
});
// Functions to handle inputs and calculations
function updateScreen() {
screen.value = currentInput;
}
function inputNumber(number) {
if (currentInput === '0' || operator) {
currentInput = number;
} else {
currentInput += number;
}
}
function inputDecimal(dot) {
if (!currentInput.includes(dot)) {
currentInput += dot;
}
}
function handleOperator(nextOperator) {
if (operator && previousInput) {
calculate();
} else {
previousInput = currentInput;
}
operator = nextOperator;
currentInput = '';
}
function calculate() {
let result = 0;
const prev = parseFloat(previousInput);
const current = parseFloat(currentInput);
if (isNaN(prev) || isNaN(current)) {
return;
}
switch (operator) {
case '+':
result = prev + current;
break;
case '-':
result = prev - current;
break;
case '*':
result = prev * current;
break;
case '/':
result = prev / current;
break;
default:
return;
}
currentInput = result.toString();
operator = null;
previousInput = '';
}
function resetCalculator() {
currentInput = '0';
previousInput = '';
operator = null;
}
Create the Files:
index.htmlstyles.cssscript.jsCopy the Code:
index.html.styles.css.script.js.Link the Files:
index.html properly links to styles.css and script.js as shown in the <head> and <body> sections.Run the Calculator:
index.html in your web browser.HTML (index.html):
CSS (styles.css):
JavaScript (script.js):
Basic Arithmetic Operations:
+), Subtraction (−), Multiplication (×), and Division (÷).Decimal Support:
. button.All Clear Function:
AC button.Chained Calculations:
Note: This calculator is a basic implementation intended for learning purposes. For enhanced functionality or more complex calculations, further development and error handling would be necessary.
Not yet included in a published release.