Code error using switch and new date

Asked

Viewed 157 times

3

I tried to do a function that would return the day of the week that we are, through a switch in JS, but somehow the result does not appear. I will debug all the code, but did not find the error.

Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> treinando js e switch</title>
</head>
<body>
     <p id="demo"> </p>


    <script>
    var day;
       function dia(){
        switch (new Date().getDay()) {
            case 0:
               day = "Segunda";
               break;
             case 1:
               day = "Terça";
               break;
             case 2:
               day = "Quarta" ;
               break;
             case 3:
               day = "Quinta";
               break;
             case 4:
               day = "Sexta";
               break;
             case 5:
               day = "Sabado";
               break;
             case 6:
               day = "Domingo";
                break;
         };
    document.getElementById('demo').innerHTLM = "hoje é "   + day;
  
    }
  

    </script>
</body>
</html>

Where am I going wrong?

2 answers

3


The main error is that 0 is Sunday, then the other days. The beginning was a bit confusing too, the variable day was declared out of the function for no apparent reason. This kind of function doing all of this is not the coolest way to do it, although it works.

function dia() {
    var day;
    switch (new Date().getDay()) {
      case 0:
        day = "Domingo";
        break;
      case 1:
        day = "Segunda";
        break;
      case 2:
        day = "Terça";
        break;
      case 3:
        day = "Quarta" ;
        break;
      case 4:
        day = "Quinta";
        break;
      case 5:
        day = "Sexta";
        break;
      case 6:
        day = "Sábado";
        break;
    };
    console.log("hoje é " + day); //mudei para facilitar o teste
}
dia();

So it can get better:

function diaDaSemanaHoje() {
    return ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"][new Date().getDay()];
}
console.log("Hoje é " + diaDaSemanaHoje());

I put in the Github for future reference.

  • Could you explain why this works?

  • @Cleitonoliveira What works?

  • @bigown , Grateful too much for the agile response, so the mistake was to start with the day of the week, was the variable being out of function? detail, really in array is more cool, only I need to train switch .... rsrsrsrs thanks

  • The outside variable even works, but is not correct. Work != correct. This is one of the most important things to learn. Variables must have contained scope: http://answall.com/q/171327/101

  • @bigown I’m sorry I didn’t clear it up. I don’t understand why Return works that way. Basically it’s a [ ][ ], one with a list of strings and the other with the current day, but I couldn’t understand how it works. I could explain?

  • 1

    @Cleitonoliveira think the explanation will get a little long for a comment, want to open a new question about it? You can even quote the answer here, put the example.

Show 1 more comment

-1

See if it helps:

var arrDay = ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"];

console.log("Hoje é " + arrDay[new Date().getDay()]);

Browser other questions tagged

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