dimanche 2 septembre 2018

main loop function doesn't run nested functions

I'm new in the JavaScript and programming world and I'm struggling with a basic JavaScript exercise, I should create with just basic functions a times table based on a user prompt value, it should a loop that interrupts just when the user insert the value -1. Although, the loop doesn't print the times table until I interrupt the loop with the -1, it seems like it's looping too fast.

Here he's my code:

var userInput;

function checkValidity(question) {

  if(isNaN(question) || (question === "" )) {
    console.log('Inserire un numero valido');
    return false;
  } else {
    console.log('defined');
    return true;
  }
}

function timeTable(valueUser) {
  document.write("Times Table for number: " + valueUser + "<br />");
  
  for(var i = 0; i < 10; i++) {                                      
    document.write(valueUser + " * " + i + " = " + valueUser * i + "<br />");
  }

}

function showQuestion() {
  userInput = parseInt(prompt("Enter your Times Table value:"));
  
  var answerValidate = checkValidity(userInput);

  if(answerValidate) {
    if(userInput !== -1) {
      timeTable(userInput);
      showQuestion();
    } else {
      console.log("Grazie per aver partecipato!");
    }
  } else {
    showQuestion();
  }

  
}

showQuestion();

Anyone that can suggest me in what I'm wrong?

Thank you everyone!

Aucun commentaire:

Enregistrer un commentaire