Human-written demonstration (OpenAssistant volunteers)· OpenAssistant (human-written)
OpenAssistant Conversations (oasst2)
cómo puede crear una calculadora usando html, css y js?
In OpenAssistant data, “assistant” turns were written by human volunteers as demonstrations. They are not model outputs.
- User
cómo puede crear una calculadora usando html, css y js?
- Assistant
A continuación te dejare un ejemplo como se puede crear una calculadora usando HTML, CSS y Javascript:
index.html
<form id="calculadora"> <table> <tr> <td colspan="4"><input type="text" readonly id="resultado"></td> </tr> <tr> <td><button type="button" id="clear" class="operacion">C</button></td> <td><button type="button" id="dividir" class="operacion">/</button></td> <td><button type="button" id="multiplicar" class="operacion">X</button></td> <td rowspan="2"><button type="button" id="restar" class="operacion">-</button></td> </tr> <tr> <td><button type="button">1</button></td> <td><button type="button">2</button></td> <td><button type="button">3</button></td> </tr> <tr> <td><button type="button">4</button></td> <td><button type="button">5</button></td> <td><button type="button">6</button></td> <td rowspan="2"><button type="button" id="sumar" class="operacion">+</button></td> </tr> <tr> <td><button type="button">7</button></td> <td><button type="button">8</button></td> <td><button type="button">9</button></td> </tr> <tr> <td colspan="2"><button type="button">0</button></td> <td colspan="2"><button type="button" id="igual" class="operacion">=</button></td> </tr> </table> </form>index.css
*{ box-sizing: border-box; } body{ background-image: linear-gradient(to left, magenta, tomato); margin: 0; min-height: 100vh; display: flex; align-items: center; justify-content: center; } #calculadora table{ border-collapse: collapse; } #calculadora table{ height: 300px; } #calculadora button, #calculadora input{ width: 100%; height: 100%; font-size: 1em; padding: 10px } #calculadora input{ padding: 15px; color: white; border: none; background-color: rgba(0, 0, 0, .5); } #calculadora input:focus{ outline: none; } #calculadora button{ background-color: rgba(0, 0, 0, .4); border: none; color: white; font-weight: bold; cursor: pointer; } #calculadora button:hover{ background-color: rgba(0, 0, 0, .5); } #calculadora button:focus{ outline: none; } #calculadora button.operacion{ background-color: crimson; } #calculadora button.operacion#restar, #calculadora button.operacion#igual, #calculadora button.operacion#clear{ background-color: steelblue; }index.js
// variables const calculadora = document.getElementById('calculadora') const resultado = document.getElementById('resultado') // eventos calculadora.addEventListener('click', añadirNumeros) // Operaciones let operaciones = [] // añadirNumeros function añadirNumeros(e){ if(e.target.getAttribute('type') === 'button'){ if(e.target.className != 'operacion'){ resultado.value += e.target.innerText } if(e.target.id === 'sumar'){ operaciones.push(resultado.value) operaciones.push('+') calculadora.reset() } if(e.target.id === 'igual'){ operaciones.push(resultado.value) calculadora.reset() const resultadoOperacion = eval(operaciones.join('')) resultado.value = resultadoOperacion operaciones = [] } if(e.target.id === 'restar'){ operaciones.push(resultado.value) operaciones.push('-') calculadora.reset() } if(e.target.id === 'multiplicar'){ operaciones.push(resultado.value) operaciones.push('*') calculadora.reset() } if(e.target.id === 'dividir'){ operaciones.push(resultado.value) operaciones.push('/') calculadora.reset() } if(e.target.id === 'clear'){ operaciones = [] calculadora.reset() } } } - User
Gracias, podrías reducir la cantidad de código que se necesita para esta calculadora?
- Assistant
Seguro! Aquí esta un ejemplo más corto de la implementación de una calculadora usando HTML, Javascript y CSS, contenido en un solo archivo de HTML.
<!DOCTYPE html> <html> <head> <script src= "https://cdnjs.cloudflare.com/ajax/libs/mathjs/10.6.4/math.js" integrity= "sha512-BbVEDjbqdN3Eow8+empLMrJlxXRj5nEitiCAK5A1pUr66+jLVejo3PmjIaucRnjlB0P9R3rBUs3g5jXc8ti+fQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> <script src= "https://cdnjs.cloudflare.com/ajax/libs/mathjs/10.6.4/math.min.js" integrity= "sha512-iphNRh6dPbeuPGIrQbCdbBF/qcqadKWLa35YPVfMZMHBSI6PLJh1om2xCTWhpVpmUyb4IvVS9iYnnYMkleVXLA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> <!-- for styling --> <style> table { border: 1px solid black; margin-left: auto; margin-right: auto; } input[type="button"] { width: 100%; padding: 20px 40px; background-color: green; color: white; font-size: 24px; font-weight: bold; border: none; border-radius: 5px; } input[type="text"] { padding: 20px 30px; font-size: 24px; font-weight: bold; border: none; border-radius: 5px; border: 2px solid black; } </style> </head> <!-- create table --> <body> <table id="calcu"> <tr> <td colspan="3"><input type="text" id="result"></td> <!-- clr() function will call clr to clear all value --> <td><input type="button" value="c" onclick="clr()" /> </td> </tr> <tr> <!-- create button and assign value to each button --> <!-- dis("1") will call function dis to display value --> <td><input type="button" value="1" onclick="dis('1')" onkeydown="myFunction(event)"> </td> <td><input type="button" value="2" onclick="dis('2')" onkeydown="myFunction(event)"> </td> <td><input type="button" value="3" onclick="dis('3')" onkeydown="myFunction(event)"> </td> <td><input type="button" value="/" onclick="dis('/')" onkeydown="myFunction(event)"> </td> </tr> <tr> <td><input type="button" value="4" onclick="dis('4')" onkeydown="myFunction(event)"> </td> <td><input type="button" value="5" onclick="dis('5')" onkeydown="myFunction(event)"> </td> <td><input type="button" value="6" onclick="dis('6')" onkeydown="myFunction(event)"> </td> <td><input type="button" value="*" onclick="dis('*')" onkeydown="myFunction(event)"> </td> </tr> <tr> <td><input type="button" value="7" onclick="dis('7')" onkeydown="myFunction(event)"> </td> <td><input type="button" value="8" onclick="dis('8')" onkeydown="myFunction(event)"> </td> <td><input type="button" value="9" onclick="dis('9')" onkeydown="myFunction(event)"> </td> <td><input type="button" value="-" onclick="dis('-')" onkeydown="myFunction(event)"> </td> </tr> <tr> <td><input type="button" value="0" onclick="dis('0')" onkeydown="myFunction(event)"> </td> <td><input type="button" value="." onclick="dis('.')" onkeydown="myFunction(event)"> </td> <!-- solve function call function solve to evaluate value --> <td><input type="button" value="=" onclick="solve()"> </td> <td><input type="button" value="+" onclick="dis('+')" onkeydown="myFunction(event)"> </td> </tr> </table> <script> // Function that display value function dis(val) { document.getElementById("result").value += val } function myFunction(event) { if (event.key == '0' || event.key == '1' || event.key == '2' || event.key == '3' || event.key == '4' || event.key == '5' || event.key == '6' || event.key == '7' || event.key == '8' || event.key == '9' || event.key == '+' || event.key == '-' || event.key == '*' || event.key == '/') document.getElementById("result").value += event.key; } var cal = document.getElementById("calcu"); cal.onkeyup = function (event) { if (event.keyCode === 13) { console.log("Enter"); let x = document.getElementById("result").value console.log(x); solve(); } } // Function that evaluates the digit and return result function solve() { let x = document.getElementById("result").value let y = math.evaluate(x) document.getElementById("result").value = y } // Function that clear the display function clr() { document.getElementById("result").value = "" } </script> </body> </html>Este ejemplo usa la función math.evaluate la biblioteca math.js en lugar de usar la función
evalde Javascript para evaluar expresiones matemáticas a calcular.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →