|
MÉTODOS MATEMÁTICOS
Contiene los métodos para realizar cualquier tipo de operación matemática compleja. Solo necesitamos llamar al método deseado utilizando el objeto Math (ya esta creado). Los métodos mas útiles son:
|
MÉTODOS MATEMÁTICOS
|
|
MÉTODO
|
DESCRIPCIÓN
|
|
var=Math.sin(valor);
|
Seno de un ángulo. Expresado en
radianes.
|
|
var=Math.cos(valor);
|
Coseno de un ángulo. Expresado en
radianes.
|
|
var=Math.tan(valor);
|
Tangente de un ángulo. Expresado en
radianes.
|
|
asin(),acos(),atan()
|
Iguales a las anteriores pero devuelve
arcos.
|
|
var=Math.abs(valor);
|
Valor absoluto (su positivo).
|
|
var=Math.log(valor);
|
Logaritmo decimal de valor.
|
|
var=Math.max(val1,val2);
|
Devuelve el mayor de los dos.
|
|
var=Math.min(val1,val2);
|
Devuelve el menor de los dos.
|
|
var=Math.pow(base,expon);
|
Potencia de base
|
|
var=Math.sqrt(valor);
|
Raíz cuadrada del valor.
|
|
var=Math.round(valor);
|
Redondea un valor con decimales.
|
|
var=Math.random()*interv;
|
Número aleatorio entre 0 y el intervalo.
|
EJEMPLO
1
<html>
<head>
<script>
function opera(valor)
{
var resultado;
var num1=parseInt(cnum1.value);
var num2=parseInt(cnum2.value);
switch(valor)
{
case 1:
resultado=Math.sqrt(Math.abs(num1));
break;
case 2:
resultado=Math.pow(num1,num2);
break;
case 3:
resultado=Math.max(num1,num2);
break;
}
resul.value=resultado;
}
</script>
</head>
<body>
NUM1:
<input type="text" name="cnum1" size="5" value="0">
<br>
NUM2:
<input type="text" name="cnum2" size="5" value="0">
<br>
<br>
<input type="button" value="Raiz" onClick=opera(1);>
<input type="button" value="Potencia" onClick=opera(2);>
<input type="button" value="Máximo" onClick=opera(3);>
<br>
<br>
Resultado:<input type="text" name="resul" size="17">
</body>
</html>
EJEMPLO 2: Creamos números aleatorios entre 1 y 10 para luego intentar acertarlo.
<html>
<head>
<script>
var secreto;
function genera()
{
secreto=Math.random()*10;
secreto=Math.round(secreto);
if(secreto==0)
secreto=10;
}
function juego()
{
var numero=parseInt(caja.value);
if(numero==secreto)
{
alert("Muy bien.A por otro");
genera();
}
else
{
if(numero<secreto)
alert("Es mayor");
else
alert("es menor");
}
caja.focus();
caja.value="";
}
</script>
</head>
<body onLoad=genera();>
NUMERO:<input type="text" name="caja" size="3">
<input type="button" value="Compara" onClick=juego();>
</body>
</html>
CONTINUAR
>
|