JavaScript - Como hacer que JS me tire una palabra random? HTML5

 
Vista:

Como hacer que JS me tire una palabra random? HTML5

Publicado por Rocio (1 intervención) el 26/11/2013 19:19:36
Hola que tal, tengo que entregar una aplicacion simple y basica, para android en html5, se me ocurrio hacer un prorgrama que te ayude a tomar decisiones, en donde vos le pongas tu problema y el programa arroje una respuesta aleatoria, y tambien debería hacer que dicha respuesta se borre al momento de ingresar una nueva pregunta. El problema es que no tengo experiencia en JS y no tengo mucha idea de cómo hacerlo. A continuacion copio el código a ver si alguien me puede dar una mano, gracias!!

HTML5:


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
<!DOCKTYPE html>
<html>
<head>
<title>Parcial HTML5</title>
<meta charset="utf-8">
<link rel="stylesheet" href="colacilli.css">
<script src="Colacilli.js"></script>
</head>
 
<body>
<header>
<h1>Decisión que vas a tomar</h1>
<link href='http://fonts.googleapis.com/css?family=Finger+Paint' rel='stylesheet' type='text/css'>
</header>
 
<article>
 
<textarea id="areadetexto" placeholder="Escribe aquí la decisíon que queres tomar..." title="Decisión" required="true"></textarea>
 
<input id="submit" type="submit" value="Enviar" onclick=decision()>
 
<section>
 
</section>
 
</article>
 
<footer>
App para Android en HTML5 - Rocio Colacilli
 
</footer>
<body>
</html>



JAVASCRIPT:


1
2
3
4
5
6
7
8
9
function decision () {
 
var respuesta = new Array ("Si", "Probablemente si", "Definitivamente no", "De ninguna manera")
 
for (i=0; i<respuesta.length; i++) {
 
document.write(respuesta[i]);
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.162
Oro
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Como hacer que JS me tire una palabra random? HTML5

Publicado por xve (2100 intervenciones) el 26/11/2013 22:12:21
Hola Rocio, para obtener un valor aleatorio, tienes que utilizar Math.random()... aqui te adjunto tu ejemplo modificado...

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
<!DOCKTYPE html>
<html>
<head>
<title>Parcial HTML5</title>
<meta charset="utf-8">
<link rel="stylesheet" href="colacilli.css">
<script>
    function decision ()
    {
        var respuesta = new Array ("Si", "Probablemente si", "Definitivamente no", "De ninguna manera")
        var aleatorio=Math.floor((Math.random()*4));
        document.getElementById("respuesta").innerHTML=respuesta[aleatorio];
        return false;
    }
</script>
</head>
 
<body>
<header>
<h1>Decisión que vas a tomar</h1>
<link href='http://fonts.googleapis.com/css?family=Finger+Paint' rel='stylesheet' type='text/css'>
</header>
 
<article>
 
    <textarea id="areadetexto" placeholder="Escribe aquí la decisíon que queres tomar..." title="Decisión" required="true"></textarea>
 
    <input type="button" value="Enviar" onclick=decision()>
 
    <div id="respuesta"></div>
</article>
 
<footer>
App para Android en HTML5 - Rocio Colacilli
 
</footer>
<body>
</html>

Coméntanos, si te sirve, ok?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar