I agree with your initial point, but not necessarily that switches are the answer.
In your example, I'd probably do something like:
conststates={foo:stateA,bar:stateB};constf=(value)=>{if(!(valueinstates)){thrownewError(`No state for ${value}`);}returnstates[value]();}
...or more generically assuming things weren't as 1-to-1...
constf=(value)=>{if(value==='foo'){returnstateA();}if(value==='bar'){returnstateB();}thrownewError(`No state for ${value}`);}
The second is probably the easiest to understand out of all of them (including the switch imo) so I'd probably lean towards that, but in every case, undefined behaviours are caught appropriately.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I agree with your initial point, but not necessarily that switches are the answer.
In your example, I'd probably do something like:
...or more generically assuming things weren't as 1-to-1...
The second is probably the easiest to understand out of all of them (including the switch imo) so I'd probably lean towards that, but in every case, undefined behaviours are caught appropriately.