My question is based solely on whether there is the possibility of entering a variable through a prompt()
and that it is saved no matter how many times the code is run afterwards, and it is deleted only when the page is reloaded or manually deleted with other user interaction. What I really want is that if the variable is introduced for the first time, it does not need to ask the user for it.
I have dealt with sessionStorage()
that due to lack of experience I do not conclude if it is my fault and I do not use it or I can not. And using something like if (variableX === null){ variableX = prompt("Introduzca el nombre.");}
(it's just an example), but nothing gives me results. then I leave an example code.
The idea is that each time the button is pressed the variable "name" is saved and reused the next time when executing trigger()
.
function trigger() {
var nombre = prompt("Introduce tu nombre");
var suma = 0;
document.getElementById("expl").innerHTML = " ";
do {
var number = prompt("Introduzca un numero para la suma.");
console.log(number);
if (Number(number) == number) {
number = Number(number);
suma = suma + number;
console.log(suma);
}else {
if (number != null){
var hola = alert(number + " No es un nuemero valido.");
console.log(hola);
}
}
} while (number != null);
document.getElementById("expl").innerHTML += nombre + ", el resultado es: " + suma;
}
<button onClick="trigger()">Haz clck para empezar</button>
<p>Click en cancelar para ver la suma</p>
<p id="expl"></p>