When you do:
switch(valor) {
case outroValor:
....
The idea is that valor
be compared with outroValor
. But case outroValor
is an expression, the outworking of this expression which is compared with valor
. Ex:
let x = 1;
switch(x) {
case x > 0:
console.log('positivo');
break;
case x < 0:
console.log('negativo');
break;
default:
console.log('nenhum');
}
switch(x)
will compare the value of x
(which is the number 1
) with the result of expressions x > 0
and x < 0
. But both are comparisons, the result of which is a boolean (true
or false
).
According to the documentation (and the language specification), switch
uses the operator ===
to compare the values.
Like the number 1
is not a boolean, he is not equal to true
, nor the false
(so much 1 === true
how much 1 === false
are false), and therefore it falls into the case default
, and the above code prints "no".
That means that the switch
whose conditions are the results of a comparison would only work if the variable being tested is boolean:
let x = 1;
let y = false;
switch (y) {
case x > 0:
console.log('positivo');
break;
case x < 0:
console.log('negativo');
break;
default:
console.log('nenhum');
}
The above code prints "negative". That’s because switch(y)
will first compare the value of y
(which is the value boolean false
) with the result of the expression x > 0
(that is true
). Afterward y
is compared with the result of x < 0
(that is false
), and how y
is also false
, the code prints "negative".
Anyway, to do what you need, use if
and else
:
function determinarGeracao(anoDeNascimento){
if (anoDeNascimento == 1945) {
return 'Geração silenciosa';
} else if (anoDeNascimento > 1945 && anoDeNascimento <= 1964) {
return 'Boomers';
} else if (anoDeNascimento > 1964 && anoDeNascimento <= 1980) {
return 'Geração X';
} else if (anoDeNascimento > 1980 && anoDeNascimento <= 1996) {
return 'Millennials';
} else if (anoDeNascimento > 1996) {
return 'Geração Z';
} else return 'opção errada';
}
console.log(determinarGeracao(1980));
First of all, notice I didn’t put console.log
within the function. console.log
returns undefined
, then it makes no sense to assign your return to a variable. Either you print, or you return this value to be printed elsewhere. So the function simply returns the text corresponding to the generation.
Another detail is that in this logic, if the person was born before 1945, is considered invalid ("wrong option"). But in the statement is thus:
Silent generation: for those born until 1945;
So actually the code should be like this:
function determinarGeracao(anoDeNascimento){
if (anoDeNascimento <= 1945) {
return 'Geração silenciosa';
}
if (anoDeNascimento <= 1964) {
return 'Boomers';
}
if (anoDeNascimento <= 1980) {
return 'Geração X';
}
if (anoDeNascimento <= 1996) {
return 'Millennials';
}
return 'Geração Z';
}
console.log(determinarGeracao(1980));
First I test whether the year is less than or equal to 1945 ("Silent Generation"). Note that you do not need the else
, for the return
already returns the value and exits the function, so if you enter any if
, the function will already return some value and will not execute the other if
's.
Then the next if
I just check if the year is less than 1964. The if
already assures me that at this point of the function the year is greater than 1945 (if it were not, would have entered the if
previous and returned), so I don’t need to put this back on if
.
The same goes for the other values, and in the last case, the "Generation Z", nor need if
, because if it got there, it is because the year is not in any of the previous cases (obviously that I’m not checking if the anoDeNascimento
is in fact a number, but you could include such verification also).
Another detail is that you were doing this:
function determinarGeracao(anoDeNascimento) {
anoDeNascimento = 1980;
...
}
That is, you receive a value as a function parameter, and then overwrite this value. I removed this line and within the function use the value that was passed the same.
In the statement it still says:
assign in the result variable one of the following conditions: ...
And maybe that’s why you’re doing resultado = 'alguma coisa'
. Well, I think this might be confusing you, because it might imply that this should be done within the function, as you’re trying to do:
function determinarGeracao(anoDeNascimento){
let resultado;
if (anoDeNascimento <= 1945) {
resultado = 'Geração silenciosa';
}
if (anoDeNascimento <= 1964) {
resultado = 'Boomers';
}
... etc....
return resultado;
}
But what I understand is that’s all you really need:
function determinarGeracao(anoDeNascimento){
if (anoDeNascimento <= 1945) {
return 'Geração silenciosa';
}
if (anoDeNascimento <= 1964) {
return 'Boomers';
}
if (anoDeNascimento <= 1980) {
return 'Geração X';
}
if (anoDeNascimento <= 1996) {
return 'Millennials';
}
return 'Geração Z';
}
let resultado = determinarGeracao(1980);
The variable resultado
receives the return of the function, and then you can do whatever you want with it (print with console.log
, for example).
Note: going a little further, numbers and other types can be "converted" to Boolean, through a mechanism known as type coercion.
Use
if
/else
instead ofswitch
, plain as that :-)– hkotsubo
Of curiosity, what would be the reason I chose precisely the
switch
for this scenario?– Bacco
Place a parenthesis in the test conditions:
( )
that will work. Example:( anoDeNascimento == 1945 )
– Ivan Ferrer
@Ivanferrer Are you sure? https://jsfiddle.net/jk5a8zg7/2/
– hkotsubo
So, try like this, should work.
– Ivan Ferrer
In fact, the switch case is being used in the wrong way, nor would be necessary, because it is a case comparator... it really won’t work this way, I thought because it works in php would also work in javascript. What actually happens is that the switch is already playing the role of if... ideone
– Ivan Ferrer
@Ivanferrer Actually in PHP "works" by coincidence, because the
switch
uses the Loose comparison, according to which any number greater than zero is equal to TRUE, so "it works". But it won’t always work: https://ideone.com/Zuq8iD– hkotsubo