Reputation: 47088
If have Stackblitz: the following typescript switch block and it executes the first case, even though the filter is false (Shown with logging statements below):
public applyFilter(filter, completeTodos, incompleteTodos): Todo[] {
console.log("FILTER VALUE: ", filter);
console.log("IS THE FILTER TRUE?", filter == true);
switch (filter) {
case filter == true:
console.log("Returning complete TODOS");
return completeTodos;
break;
case filter == false:
console.log("Returning incomplete TODOS");
return incompleteTodos;
break;
default:
console.log("Returning the Default");
return completeTodos;
}
}
This is what the console logs (Can be seen in stackblitz link):
FILTER VALUE:
false
IS THE FILTER TRUE?
false
Returning complete TODOS
I have a feeling I'm missing something really simple, but at the same time this just seems wrong ... Thoughts?
Upvotes: 2
Views: 2910
Reputation: 4330
Refer how switch case work:
This is how it works:
The switch expression is evaluated once. The value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed.
Your case filter == true is returning false
Change your code like this:
public applyFilter(filter, completeTodos, incompleteTodos): Todo[] {
console.log("FILTER VALUE: ", filter);
console.log("IS THE FILTER TRUE?", filter == true);
switch (filter) {
case true:
console.log("Returning complete TODOS");
return completeTodos;
break;
case false:
console.log("Returning incomplete TODOS");
return incompleteTodos;
break;
default:
console.log("Returning the Default");
return completeTodos;
}
}
Upvotes: 0
Reputation: 17587
That is not how you write switch cases. The whole idea is to switch on a dynamic value and then let each case handle a static value.
switch (filter) {
case true:
console.log("Returning complete TODOS");
return completeTodos;
case false:
console.log("Returning incomplete TODOS");
return incompleteTodos;
default:
console.log("Returning the Default");
return completeTodos;
}
The break
statements are redundant too since you return before them. You only need break
if you want to continue execution after the switch block.
Also, your default case is kinda moot since a bool is either true or false, unless you plan to pass undefined
in some case.
Upvotes: 4
Reputation: 17626
The idea of a switch is to use the value within the switch(someValue)
and check it within each case, but you're manually doing each case checking.
If you wish to keep it this way, put true in the switch(true)
const filter = false;
switch (true) {
case filter == true:
console.log("Returning complete TODOS");
break;
case filter == false:
console.log("Returning incomplete TODOS");
break;
default:
console.log("Returning the Default");
}
Otherwise, use the switch statement like so:
const filter = false;
switch (filter) {
case true:
console.log("Returning complete TODOS");
break;
case false:
console.log("Returning incomplete TODOS");
break;
default:
console.log("Returning the Default");
}
Upvotes: 1