質問

I'm a new JavaScript programmer and I recently am creating a user ID page (basicly it says your name lenght, your age and, supposedly your sign.

Now the problem is that, when I'm creating the sign part ( function mySigno() ) I inserted 2 possible months (Janeiro - January and Fevereiro - February) but I insert the same «if» for Março - March, the function doesn't even work.

<center><button type="button" onclick="mySigno()">Qual é o meu signo?</button></center>

<script>
  function mySigno()
  {
    var Signo_baseado_no_mes = prompt("Em que mês nasceste? (ex.: Julho)")

    if(Signo_baseado_no_mes === "Janeiro")
    {
      var Signo_no_mes_de_Janeiro = prompt("Em que dia de Janeiro nasceste? (ex.: 16)")
    }

    if(Signo_no_mes_de_Janeiro <= 20)
    {
      alert("O teu signo é Capricórnio!")
    }

    if(Signo_no_mes_de_Janeiro >= 21)
    {
      alert("O teu signo é Aquário!")
    }

    if(Signo_baseado_no_mes === "Fevereiro")
    {
      var Signo_no_mes_de_Fevereiro = prompt("Em que dia de Fevereiro nasceste? (ex.: 18)")
    }

    if(Signo_no_mes_de_Fevereiro <= 19)
    {
      alert("O teu signo é Aquário!")
    }

    if(Signo_no_mes_de_Fevereiro >= 20)
    {
      alert("O teu signo é Peixes!")
    }

    if(Signo_baseado_no_mes === "Março")
    {
      var Signo_no_mes_de_Março = prompt("Em que dia de Março nasceste? (ex.: 4)")
    }

    if(Signo_no_mes_de_Março <= 20)
    {
      alert("O teu signo é Peixes!")
    }

    if(Signo_no_mes_de_Março >= 21)
    {
      alert("O teu signo é Áries!")
    }
  }
</script>

After laughing about my code, please try to explain what is wrong, thx!

P.S.: Sorry about all the Portuguese stuff, there is no problem changing any text to English when answering (and also sorry for possible bad English).

役に立ちましたか?

解決

Refactored your code a bit - it now works as expected. Will second @jgitter's opinion as well.

function mySigno() {   
  var msg = 'Em que dia de %s nasceste? (ex.: 16)';
  var month = window.prompt('Em que mês nasceste? (ex.: Julho)');
  var day = window.prompt(msg.replace(/%s/, month));
  day = parseInt(day, 10);

  switch (month) {
  case 'Janeiro':
  case 'janeiro':
    if (day <= 20) {
      window.alert('O teu signo é Capricórnio!');
    } else {
      window.alert('O teu signo é Aquário!');
    }
    break;
  case 'Fevereiro':
  case 'fevereiro':
    if (day <= 19) {
      window.alert('O teu signo é Aquário!');
    } else {
      window.alert('O teu signo é Peixes!');
    }
    break;
  case 'Março':
  case 'março':
    if (day <= 20) {
      window.alert('O teu signo é Peixes!');
    } else {
      window.alert('O teu signo é Áries!');
    }
    break;
  }
}

他のヒント

The direct answer to your question is that you can use as many if statements as you need to in JavaScript. You should realize, however, that in your example it is evaluating every single one of those if statements.

Since the two statements that follow each month only need to be used if it's that month, you should put them within the if statement as such:

if(Signo_baseado_no_mes === "Janeiro")
{
  // only do these things if Janeiro
  var Signo_no_mes_de_Janeiro = prompt("Em que dia de Janeiro nasceste? (ex.: 16)")


  if(Signo_no_mes_de_Janeiro <= 20)
  {
      alert("O teu signo é Capricórnio!")
  }
  if(Signo_no_mes_de_Janeiro >= 21)
  {
      alert("O teu signo é Aquário!")
  }

 }

That doesn't solve the function not working but it will at least run the least amount of code. Also keep in mind that JavaScript creates the variables that you declare inside of the if statements even if you don't get to them. This happens automatically. It shouldn't be an issue in your code right now but it will error if you have two identically named variables declared with 'var' even though they are separated by if blocks.

Lastly, because you are saying if === "Janeiro" and similar, the user's input MUST match it exactly. Uppercase must be the same as well or else it won't match!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top