欧博官网¿Como puedo añadir las funciones de
Antes de responder debo de aclarar que hacer el uso de eval no es recomendable por problemas de seguridad y performance, y la forma en que está hecha la calculadora es un tanto arcaica pero para efectos de aprendizaje suopongo que está bien.
De la forma en la que estas haciendolo, cuando presiones el boton de sin, por ejemplo, debes de inyectar la funcion de Math.sin( a tu display al inicio y agregar un parentesis al final.
if(character == "sin") input.value = "Math.sin(" + input.value + ")";En tu validación tienes que agregar el caracter punto, despues si encuentra una M tienes que validar si se trata de la palabra Math, en caso afirmativo recorres tu i para leer el siguiente caracter después de Math De igual forma si encuentra una s validas que se trate de la palabra sin y de nuevo recorres tu i Esete último proceso lo repites con cos, log2, log10, etc.
if(ch=="M" && str.substring(i, i+4) == "Math") i+=3; else if(ch=="s" && str.substring(i, i+3) == "sin") i+=2; else if(ch=="l" && str.substring(i, i+4) == "log2") i+=3; else { alert("invalid entry!") return false }Te pongo tu ejemplo modificado únicamente para sin y log2
function addChar(input, character) { if(character == "sin") input.value = "Math.sin(" + input.value + ")"; else if(character == "log2") input.value = "Math.log2(" + input.value + ")"; else input.value += character } function deleteChar(input) { input.value = input.value.substring(0, input.value.length - 1) } function changeSign(input) { if(input.value.substring(0, 1) == "-") input.value = input.value.substring(1, input.value.length) else input.value = "-" + input.value } function compute(form) { form.display.value = eval(form.display.value) } function square(form) { form.display.value = eval(form.display.value) * eval(form.display.value) } function checkNum(str) { for (var i = 0; i < str.length; i++) { var ch = str.substring(i, i+1) if (ch < "0" || ch > "9") { if (ch != "/" && ch != "*" && ch != "+" && ch != "-" && ch != "(" && ch!= ")" && ch!= "." ) { if(ch=="M" && str.substring(i, i+4) == "Math") i+=3; else if(ch=="s" && str.substring(i, i+3) == "sin") i+=2; else if(ch=="l" && str.substring(i, i+4) == "log2") i+=3; else { alert("invalid entry!") return false } } } } return true } <DOCTYPE html> <html> <head> <title>Calculadora Web</title> <link type="text/css" href="estilo.css"> </head> <body> <center> <div> <form> <table> <tr> <td colspan="4"><input type="text" value=""></td> </tr> <tr> <td><input type="button" value="sin"></td> <td><input type="button" value="cos"></td> <td><input type="button" value="tan"></td> <td><input type="button" value="√"></td> </tr> <tr> <td><input type="button" value="log10"></td> <td><input type="button" value="log2"></td> <td><input type="button" value="x²"></td> <td><input type="button" value="π"></td> </tr> <tr> <td><input type="button" value="7"></td> <td><input type="button" value="8"></td> <td><input type="button" value="9"></td> <td><input type="button" value="+"></td> </tr> <tr> <td><input type="button" value="4"></td> <td><input type="button" value="5"></td> <td><input type="button" value="6"></td> <td><input type="button" value="-"></td> </tr> <tr> <td><input type="button" value="1"></td> <td><input type="button" value="2"></td> <td><input type="button" value="3"></td> <td><input type="button" value="*"></td> </tr> <tr> <td><input type="button" value="CE"></td> <td><input type="button" value="0"></td> <td><input type="button" value="="></td> <td><input type="button" value="/"></td> </tr> </table> </form> </div> </center> </body> </html>