Message from JavaScript discussions

October 2020

— 

const weatherCheck = strength => [
{ strengths: [1,2,3], name: 'Windy' },
{ strengths: [4,5,6], name: 'Hurricanes' },
{ strengths: [7,8,9], name: 'Droughts' },
].find(type => type.strengths.find(s => s == strength)).name

weatherCheck(4) // Hurricanes

Message permanent page

— 

Or if you have a 1:1 mapping:

const weatherCheck = strength => ({
1: 'windy',
2: 'turbulent',
3: 'chaotic',
}[strength])

— Defaults are easy too. if no value is found. just do || 'Sunny?'

— This is a great example misan...

— Great going

— Switchs are easier when you need to change your whole logic based on a value

— Change your whole logic?

— (req, res) => {
switch (req.method) {
case 'GET':
cleanupForGet(req, res);
return handleGet(req.url, res);
case 'POST':
const items = postParser(req);
poster(req, ...items);
res.end('OK');
default:
res.setStatus(404);
res.end('404 Not Found');
}
}

Message permanent page

— Switch is more readable for simple/unique/one/place choice

— Maps are better for scattered/multi-places/non-trivial/reuse

— This already has a bug

— Because switches are so bad