Simplify nested if/else with repeated results?
You could check with the result of the first check.
This is an exclusive OR check.
// typeof inverse === 'boolean'function handleDirection(src) { if (src === 'left' === inverse) { tracker--; } else { tracker++; }}
The check evaluates the expression in this order (src === 'left') === inverse
:
src === 'left' === inverse---- first --- returns a boolean value--------- second --------- take result of former check & compairs it with another boolean
function handleDirection(src) { var movement = 1; if(src === 'left') movement = -1; if(inverse) tracker += movement; else tracker -= movement;}
You can even do it with just one line of Code:
function getDirectionOffset(src) { tracker += (src === 'left' ? 1 : -1) * (inverse ? -1 : 1);}