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?
Could you explain why this works?
– Cleiton Oliveira
@Cleitonoliveira What works?
– Maniero
@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
– Jean Plehiers
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
– Maniero
@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?
– Cleiton Oliveira
@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.
– Maniero