-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs08-funciones.html
49 lines (45 loc) · 1.3 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
49
<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());
}
//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?
/*
En Chrome y Firefox no, al no haber tipos JS se traga casi todo
Al concatenar con una cadena, imprime la cadena de S
Ejecutado con y sin toString, igual resultado.
*/
//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();">
<input type="button" value="Mostrar Suma 10+20" onclick="Sumar(10,20);">
<input type="button" value="Mostrar Suma numero variable de argumentos" onclick="Sumar2(10,20,3,5,100);">
<input type="button" value="Mostrar Resta" onclick="alert('Resta='+Restar(10,20));">
</body>
</html>