Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Solución con JS y sessionStore</title>
  6. </head>
  7. <body>
  8.  
  9. <!-- 1. tomar el valor de una variable que viene solamente por URL(GET), llamada 'id'
  10.  
  11. Para evitar php, elegí la opción de usar un "input" para tomar el dato "id".
  12. -->
  13.  
  14. <p>Ingrese un número y haga click en Evaluar:</p>
  15.  
  16. <input id="id1" type="number" min="0" required>
  17. <button id="boton" onclick="evaluarCondicion()">Evaluar</button>
  18.  
  19. <script>
  20.  
  21. /* 2. utilizar esta variable y condicionar:
  22.  
  23. a. si es superior y solo igual a 2, tomando números pares únicamente hasta llegar a 10
  24. b. tienen que ser números enteros(int), sin valores decimales, y pares */
  25.  
  26. function cumpleCondicion ($valor) {
  27. return ($valor >= 2) && ($valor % 2 == 0) && ($valor < 10) && (parseInt($valor) == $valor);
  28. }
  29.  
  30. /* 3. agarrar este output, y guardarlo en algún array, de tal manera que al volver a ingresar otro valor por URL me haya guardado el anterior, y así consecutivamente(SESIONES/COOKIES es un ejemplo) */
  31.  
  32. function evaluarCondicion() {
  33. var idObj = document.getElementById("id1");
  34. if (!idObj.checkValidity()) {
  35. document.getElementById("mostrarIds").innerHTML = idObj.validationMessage;
  36. } else {
  37. document.getElementById("id1").disabled = true;
  38. document.getElementById("boton").disabled = true;
  39.  
  40. if (cumpleCondicion(idObj.value)) {
  41. var arreglo = JSON.parse(sessionStorage.getItem("arregloIds"));
  42. if (arreglo == null) {
  43. arreglo = [];
  44. }
  45. arreglo.push(idObj.value);
  46. sessionStorage.setItem("arregloIds", JSON.stringify(arreglo));
  47. }
  48. mostrarIdsAlmacenados();
  49. }
  50. }
  51.  
  52. /* 4. al final del script, explorar el array guardado con los valores ingresados anteriormente (si ingresé varias veces a la URL con diferentes ID, por ejemplo), y mostrar líneas de resultado condicionando:
  53. a. 1 línea por resultado
  54. b. las líneas tienen que tener breakline (<br> o \n)
  55. c. la linea tiene que seguir el siguiente formato: "Ingresé al array y tengo el ID: $id" */
  56.  
  57. function mostrarIdsAlmacenados () {
  58. document.getElementById("mostrarIds").innerHTML = "";
  59. var arreglo = JSON.parse(sessionStorage.getItem("arregloIds"));
  60. if (arreglo != null) {
  61. for (var i = 0; i < arreglo.length ; i++) {
  62. var id = arreglo[i];
  63. document.getElementById("mostrarIds").innerHTML += "Ingresé al array y tengo el ID: "+id+"<br>";
  64. }
  65. }
  66. }
  67.  
  68. </script>
  69.  
  70. <p id="mostrarIds"></p>
  71.  
  72. </body>
  73. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement