dimanche 5 août 2018

Is this a correct loop back to my original prompt function? (javascript)

I have a really simple Javascript function that's based off of a variable taken from a prompt. If the input in the prompt is neither a number nor a string, I want it to automatically loop back to the prompt so the user can write the input again. Is this code correct for that?

var a = prompt("please enter a number");
function tys() {
    if (typeof a == 'string') {
        document.write("You exited.");
    } else if (isNaN(a)) {
        document.write(a-10);
    } else {
        document.write("Input invalid. Please write another input.");
        tys();
    }
}

Aucun commentaire:

Enregistrer un commentaire