Foros del Web » Programando para Internet » Javascript »

Calculadora en JavaScript

Estas en el tema de Calculadora en JavaScript en el foro de Javascript en Foros del Web. Hola, Les escribo para que me ayuden con este código ya que me sale error y no puedo solucionarlo. Y otro problema que tengo es ...
  #1 (permalink)  
Antiguo 11/11/2009, 08:55
 
Fecha de Ingreso: septiembre-2009
Mensajes: 13
Antigüedad: 14 años, 7 meses
Puntos: 0
Calculadora en JavaScript

Hola,

Les escribo para que me ayuden con este código ya que me sale error y no puedo solucionarlo. Y otro problema que tengo es que quisiera saber si dentro del function cuando pongo document.calculadora.display.value deberia poner el nombre del formulario que en mi caso es form1. Espero repuestas. Desde ya muchas gracias.


P.D.: a continuación les dejo el cógigo que realize. Saludos.
  #2 (permalink)  
Antiguo 11/11/2009, 08:57
 
Fecha de Ingreso: septiembre-2009
Mensajes: 13
Antigüedad: 14 años, 7 meses
Puntos: 0
Respuesta: Calculadora en JavaScript

<html>
<head>
<title>
Calculadora
</title>
<link rel="stylesheet" type="text/css" href="estilo1.css" media="screen">
<script language="javascript">
var numero = new Number (0);
var calculo = new Number (0);
var pantalla = new String ();
var operacion = new String ();

function borrar () {
numero=0;
calculo=0;
pantala="";
document.calculadora.display.value="0";
}
function visor () {
if (pantalla=="") {
document.calculadora.display.value="";
}
}
function punto () {
if (pantalla=="")
{
pantalla="0.";
document.calculadora.display.value=pantalla;
}
else{
pantalla=document.calculadora.display.value
document.calculadora.display.value=pantalla + ".";
}
}
function actualizar () {
form1.calculadora.display.value=pantalla;
}
function calcular () {
if (operacion=="sumar") {
if (pantalla!="") {
numero=parseFloat (pantalla);
}
else
{
numero=0;
}
calculo=calculo + numero;
document.calculadora.display.value=calculo
pantalla="";
}
if (operacion=="restar") {
if (pantalla!=""){
numero=parseFloat (pantalla);
}
else
{
numero=0;
}
calculo=calculo - numero;
document.calculadora.display.value=calculo
pantalla="";
}
if (operacion=="multiplicar") {
if (pantalla!="") {
numero=parseFloat (pantalla);
}
else
{
numero=0;
}
calculo=calculo * numero;
document.calculadora.display.value=calculo
pantalla="";
}
if (pantalla=="dividir") {
if (pantalla!="") {
numero=parseFloat (pantalla);
}
else{
numero=0;
}
if (pantalla==0) {
document.calculadora.display.value="Error Division por Cero";
pantalla="";
else{

calculo=calculo / numero;
document.calculadora.display.value=calculo;
pantalla="";

}
}
if (operacion=="") {
if (pantalla=="") {
calculo=0;
}
else {
calculo=parseFloat (pantalla);
pantalla="";
}
}
}
function final () {
calcular ();
operacion="";
calculo=0;
}
</script>
</head>
<body>
<center><h2><b><u>Calculadora:</u></b></h2></center>
<br>
<br>
<br>
<br>
<form action="" method="GET" name="form1" enctype="text/plain">
<table colspan="1" width="128" height="23" align="center" cellspacing="0" cellpadding="0" border="#cccccc">
<tr>
<td>
<input type="text" name="display" value="0" class="display" readonly="readonly" style="width:128px;height:23px;text-align:right;color:red">
</td>
</tr>
<table border="1" colspan="3" align="center" cellspacing="0" cellpadding="0" border="#cccccc" height="25" width="30">
<tr>
<td colspan="3">
<input type="button" name="borrado" value="C" size="width:30px;height:24px" onclick="borrar();">
</td>
<td>
<input type="button" name="borradogeneral" value="CE" size="width:30px;height:24px" onclick="borrar();">
</td>
<td>
<input type="button" name="retr" value="Retro" size="width:30px;height:24px">
</td>
</tr>
</table>
</table>
<table border="1" colspan="14" align="center" cellspacing="0" cellpadding="0" border="#cccccc" height="100" width="100">
<tr>
<td>
<input type="button" name="1" value="1" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 1; actualizar();">
</td>
<td>
<input type="button" name="2" value="2" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 2; actualizar();">
</td>
<td>
<input type="button" name="3" value="3" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 3; actualizar();">
</td>
<td>
<input type="button" name="numero" value="." style="width:30; height:24" onclick="punto();">
</td>
</tr>
<td>
<input type="button" name="4" value="4" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 4; actualizar();">
</td>
<td>
<input type="button" name="5" value="5" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 5; actualizar();">
</td>
<td>
<input type="button" name="6" value="6" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 6; actualizar();">
</td>
<td>
<input type="button" name="igual" value="=" style="width:30; height:24" onclick="final();">
</td>
</tr>
<tr>
<td>
<input type="button" name="7" value="7" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 7; actualizar();">
</td>
<td>
<input type="button" name="8" value="8" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 8; actualizar();">
</td>
<td>
<input type="button" name="9" value="9" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 9; actualizar();">
</td>
<td>
<input type="button" name="rest" value="-" style="width:30; height:24" onclick="calcular(); operacion='restar';">
</td>
</tr>
<tr>
<td>
<input type="button" name="numero" value="0" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 0; actualizar();">
</td>
<td>
<input type="button" name="multi" value="*" style="width:30; height:24" onclick="calcular(); operacion='multiplicar';">
</td>
<td>
<input type="button" name="div" value=" / " style="width:30; height:24" onclick="calcular(); operacion='dividir';">
</td>
<td>
<input type="button" name="mas" value="+" style="width:30; height:24" onclick="calcular(); operacion='sumar';">
</td>
</tr>
</tr>
</table>
</form>
</body>
</html>
  #3 (permalink)  
Antiguo 11/11/2009, 09:01
 
Fecha de Ingreso: septiembre-2009
Mensajes: 13
Antigüedad: 14 años, 7 meses
Puntos: 0
Respuesta: Calculadora en JavaScript

<html>
<head>
<title>
Calculadora
</title>
<link rel="stylesheet" type="text/css" href="estilo1.css" media="screen">
<script language="javascript">
var numero = new Number (0);
var calculo = new Number (0);
var pantalla = new String ();
var operacion = new String ();

function borrar () {
numero=0;
calculo=0;
pantala="";
document.calculadora.display.value="0";
}
function visor () {
if (pantalla=="") {
document.calculadora.display.value="";
}
}
function punto () {
if (pantalla=="")
{
pantalla="0.";
document.calculadora.display.value=pantalla;
}
else{
pantalla=document.calculadora.display.value
document.calculadora.display.value=pantalla + ".";
}
}
function actualizar () {
form1.calculadora.display.value=pantalla;
}
function calcular () {
if (operacion=="sumar") {
if (pantalla!="") {
numero=parseFloat (pantalla);
}
else
{
numero=0;
}
calculo=calculo + numero;
document.calculadora.display.value=calculo
pantalla="";
}
if (operacion=="restar") {
if (pantalla!=""){
numero=parseFloat (pantalla);
}
else
{
numero=0;
}
calculo=calculo - numero;
document.calculadora.display.value=calculo
pantalla="";
}
if (operacion=="multiplicar") {
if (pantalla!="") {
numero=parseFloat (pantalla);
}
else
{
numero=0;
}
calculo=calculo * numero;
document.calculadora.display.value=calculo
pantalla="";
}
if (pantalla=="dividir") {
if (pantalla!="") {
numero=parseFloat (pantalla);
}
else{
numero=0;
}
if (pantalla==0) {
document.calculadora.display.value="Error Division por Cero";
pantalla="";
else{

calculo=calculo / numero;
document.calculadora.display.value=calculo;
pantalla="";

}
}
if (operacion=="") {
if (pantalla=="") {
calculo=0;
}
else {
calculo=parseFloat (pantalla);
pantalla="";
}
}
}
function final () {
calcular ();
operacion="";
calculo=0;
}
</script>
</head>
<body>
<center><h2><b><u>Calculadora:</u></b></h2></center>
<br>
<br>
<br>
<br>
<form action="" method="GET" name="form1" enctype="text/plain">
<table colspan="1" width="128" height="23" align="center" cellspacing="0" cellpadding="0" border="#cccccc">
<tr>
<td>
<input type="text" name="display" value="0" class="display" readonly="readonly" style="width:128px;height:23px;text-align:right;color:red">
</td>
</tr>
<table border="1" colspan="3" align="center" cellspacing="0" cellpadding="0" border="#cccccc" height="25" width="30">
<tr>
<td colspan="3">
<input type="button" name="borrado" value="C" size="width:30px;height:24px" onclick="borrar();">
</td>
<td>
<input type="button" name="borradogeneral" value="CE" size="width:30px;height:24px" onclick="borrar();">
</td>
<td>
<input type="button" name="retr" value="Retro" size="width:30px;height:24px">
</td>
</tr>
</table>
</table>
<table border="1" colspan="14" align="center" cellspacing="0" cellpadding="0" border="#cccccc" height="100" width="100">
<tr>
<td>
<input type="button" name="1" value="1" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 1; actualizar();">
</td>
<td>
<input type="button" name="2" value="2" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 2; actualizar();">
</td>
<td>
<input type="button" name="3" value="3" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 3; actualizar();">
</td>
<td>
<input type="button" name="numero" value="." style="width:30; height:24" onclick="punto();">
</td>
</tr>
<td>
<input type="button" name="4" value="4" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 4; actualizar();">
</td>
<td>
<input type="button" name="5" value="5" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 5; actualizar();">
</td>
<td>
<input type="button" name="6" value="6" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 6; actualizar();">
</td>
<td>
<input type="button" name="igual" value="=" style="width:30; height:24" onclick="final();">
</td>
</tr>
<tr>
<td>
<input type="button" name="7" value="7" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 7; actualizar();">
</td>
<td>
<input type="button" name="8" value="8" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 8; actualizar();">
</td>
<td>
<input type="button" name="9" value="9" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 9; actualizar();">
</td>
<td>
<input type="button" name="rest" value="-" style="width:30; height:24" onclick="calcular(); operacion='restar';">
</td>
</tr>
<tr>
<td>
<input type="button" name="numero" value="0" style="width:30; height:24" onclick="visor(); pantalla=pantalla + 0; actualizar();">
</td>
<td>
<input type="button" name="multi" value="*" style="width:30; height:24" onclick="calcular(); operacion='multiplicar';">
</td>
<td>
<input type="button" name="div" value=" / " style="width:30; height:24" onclick="calcular(); operacion='dividir';">
</td>
<td>
<input type="button" name="mas" value="+" style="width:30; height:24" onclick="calcular(); operacion='sumar';">
</td>
</tr>
</tr>
</table>
</form>
</body>
</html>
  #4 (permalink)  
Antiguo 11/11/2009, 09:12
Avatar de David
Moderador
 
Fecha de Ingreso: abril-2005
Ubicación: In this planet
Mensajes: 15.720
Antigüedad: 19 años
Puntos: 839
Respuesta: Calculadora en JavaScript

Tienes un error en la línea:
Código Javascript:
Ver original
  1. else {
Debería ser:
Código Javascript:
Ver original
  1. } else {
Además, no existe ningún elemento llamado "calculadora"
__________________
Por favor, antes de preguntar, revisa la Guía para realizar preguntas.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:26.