mercredi 21 novembre 2018

trying to make nr restriction 1-10, but prompt allows users to pick 13

so i am trying to make a tick system, where i a user can pick a number from 1-10 and there will start a countdown from the number that the user picks to 0. The problem is that when a user picks number 12 in prompt, it doesnt allow him/her to continue, but when they try again it allows them.

html:

 <button onclick=start()>Countdown</button>
   <p id="p">

scirpt:

<script>
var ticks;
var mytimer;

function start() {
    ticks = Number(prompt("where do you want to start a countdown, min 1 max 10")) + 1;
    if (ticks > 11) {
        ticks = Number(prompt("Skriv inn på nytt, tallet må være mellom 3-10")) + 1;
    }
    else if (ticks < 1) {
        ticks = Number(prompt("Try again, The number must be between 1-10")) + 1;
    }
    mytimer = setInterval(tick, 1000)

}

  function tick() {
    ticks--;
    document.getElementById("p").innerHTML = "Tick nr. " + ticks + "<br />";

    if (ticks === 1) {
        clearInterval(mytimer);
        document.write("Done!");
    }
}
  </script>

Aucun commentaire:

Enregistrer un commentaire