-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs08-funciones.html
48 lines (44 loc) · 1.38 KB
/
js08-funciones.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<html>
<head>
<title>Funciones 2</title>
<script type="text/javascript">
//Función sencilla
function MostrarMsg(){
alert("Esto es un mensaje");
}
//Función con parámetros y conversión de tipos
function Sumar(a,b){
//alert("Suma="+(a+b).toString());
alert("Suma con parentesis="+(a+b)); //OK
alert("Suma sin parentesis="+a+b);
}
//Función con un número variable de parámetros
//para ello hace uso de la función arguments
function Sumar2(){
var s=0;
for (var i=0; i<arguments.length;i++)
{
s = s + arguments[i];
}
alert("Suma="+s);
}
//Comprueba el funcionamiento de Sumar2 en otros navegadores
// Es necesario utilizar toString() al concatenar la variable s
// dentro del alert?
// No, no es necesario siempre y cuando hagamos uso de los paréntesis
//Función que devuelve un valor
function Restar(a,b){
var c=0;
c=a-b;
return c;
}
</script>
</head>
<body>
<input type="button" value="Mostrar Mensaje" onclick="MostrarMsg();"> <br/>
<input type="button" value="Mostrar Suma 10+20" onclick="Sumar(10,20);"> <br/>
<input type="button" value="Mostrar Suma numero variable de argumentos" onclick="Sumar2(10,20);"> <br/>
<!-- En este último caso, el alert está definido dentro del onClick() -->
<input type="button" value="Mostrar Resta" onclick="alert('Resta='+Restar(20,10));"> <br/>
</body>
</html>