Null input via keyboard in a variable receipt via Javascript

Asked

Viewed 91 times

-1

Good night! In my practical Javascript experiences, I tried to create a condition to alert the user about a variable he missed by pressing the key enter twice. However, in trying to solve this problem, I made two attempts:

/*Tentativa 1:*/

var linha = Array(3,3);
var x, y;
for (x = 0; x < 3; x++) {
  for (y = 0; y < 3; y++) {
    linha[x,y] = parseInt(prompt("Digite o "+[y+1]+"º número da "+[x+1]+"º coluna"));
    if (linha[x,y] == null) {
      alert("Digite alguma coisa");
      while (linha[x,y] == null){
        linha[x,y] = parseInt(prompt("Digite o "+[y+1]+"º número da "+[x+1]+"º coluna"));
        if (linha[x,y] == null) {
          alert("Digite alguma coisa");
        }
      }
    }
  }
}

/*Tentativa 2:*/

var linha = Array(3,3);
var x, y;
for (x = 0; x < 3; x++) {
  for (y = 0; y < 3; y++) {
    linha[x,y] = parseInt(prompt("Digite o "+[y+1]+"º número da "+[x+1]+"º coluna"));
    if (linha[x,y] == " ") {
      alert("Digite alguma coisa");
      while (linha[x,y] == " "){
        linha[x,y] = parseInt(prompt("Digite o "+[y+1]+"º número da "+[x+1]+"º coluna"));
        if (linha[x,y] == " ") {
          alert("Digite alguma coisa");
        }
      }
    }
  }
}

Question: Which method is most feasible to solve this problem above?

  • Set this problem aside a little and remove the snippets of code you entered trying to solve this. Make the rest work first. You may think you are working, but you are not. Run this code in a Debugger or REPL and enter the values of the variables. Your array is being initialized incorrectly and linha[x,y] doesn’t do what you think.

  • But @bfavaretto, in this case I’m assembling an array with Array(). If I put the project proposal related to this algorithm that I’m mounting here on the forum, the moderators will cancel the post.

1 answer

1


When converting null, or an empty string, with parseInt, the assigned value will be Nan (Not-a-Number). Therefore, the safest possible way to test if the user has not entered any value is by using the global function isNaN. She returns true if the tested value is not a number (isnot-a-Number). Then I’d be:

var linha = Array(3,3); 
var x, y; 
for (x = 0; x < 3; x++) { 
    for (y = 0; y < 3; y++) { 
        linha[x,y] = parseInt(prompt("Digite o "+[y+1]+"º número da "+[x+1]+"º coluna")); 
        if (isNaN(linha[x,y])) { 
            alert("Digite alguma coisa");
            while (isNaN(linha[x,y])){ 
                linha[x,y] = parseInt(prompt("Digite o "+[y+1]+"º número da "+[x+1]+"º coluna")); 
                if (isNaN(linha[x,y])) { 
                    alert("Digite alguma coisa"); 
                } 
            } 
        } 
    } 
}

According to what you’ve made available, I think this might solve the problem.

I hope I’ve helped!

  • Gratitude! Now the algorithm will not let the user close it without typing the values that logic asks. Thanks!! ;-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.