Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>Solución con JS y sessionStore</title>
- </head>
- <body>
- <!-- 1. tomar el valor de una variable que viene solamente por URL(GET), llamada 'id'
- Para evitar php, elegí la opción de usar un "input" para tomar el dato "id".
- -->
- <p>Ingrese un número y haga click en Evaluar:</p>
- <input id="id1" type="number" min="0" required>
- <button id="boton" onclick="evaluarCondicion()">Evaluar</button>
- <script>
- /* 2. utilizar esta variable y condicionar:
- a. si es superior y solo igual a 2, tomando números pares únicamente hasta llegar a 10
- b. tienen que ser números enteros(int), sin valores decimales, y pares */
- function cumpleCondicion ($valor) {
- return ($valor >= 2) && ($valor % 2 == 0) && ($valor < 10) && (parseInt($valor) == $valor);
- }
- /* 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) */
- function evaluarCondicion() {
- var idObj = document.getElementById("id1");
- if (!idObj.checkValidity()) {
- document.getElementById("mostrarIds").innerHTML = idObj.validationMessage;
- } else {
- document.getElementById("id1").disabled = true;
- document.getElementById("boton").disabled = true;
- if (cumpleCondicion(idObj.value)) {
- var arreglo = JSON.parse(sessionStorage.getItem("arregloIds"));
- if (arreglo == null) {
- arreglo = [];
- }
- arreglo.push(idObj.value);
- sessionStorage.setItem("arregloIds", JSON.stringify(arreglo));
- }
- mostrarIdsAlmacenados();
- }
- }
- /* 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:
- a. 1 línea por resultado
- b. las líneas tienen que tener breakline (<br> o \n)
- c. la linea tiene que seguir el siguiente formato: "Ingresé al array y tengo el ID: $id" */
- function mostrarIdsAlmacenados () {
- document.getElementById("mostrarIds").innerHTML = "";
- var arreglo = JSON.parse(sessionStorage.getItem("arregloIds"));
- if (arreglo != null) {
- for (var i = 0; i < arreglo.length ; i++) {
- var id = arreglo[i];
- document.getElementById("mostrarIds").innerHTML += "Ingresé al array y tengo el ID: "+id+"<br>";
- }
- }
- }
- </script>
- <p id="mostrarIds"></p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement