onlycode.org

javascript
jquery
html
angularjs
css
node.js
ajax
html5
php
json
arrays
google-chrome
dom
regex
reactjs
ecmascript-6
javascript-events

your's history:
simplify an ifelse state...

Simplify an if/else statement, just only code?

if-statement | jquery | javascript | conditional

edit
// typeof inverse === 'boolean'

function handleDirection(src) {
    if (src === 'left' === inverse) {
        tracker--;
    } else {
        tracker++;
    }
}

34
Nina Scholz, 13.12.2018 12:29




edit
function handleDirection(src) {
   var i = 1;
   if(src === 'left')
     i = -1;

   if(inverse)
     tracker += i;
   else
     tracker -= i;
}

32
Alays, 13.12.2018 12:30




edit
function handleDirection(src) {
  var delta = (src === 'left' && inverse) || (src !== 'left' && !inverse) ? -1 : 1;
  tracker += delta;
}
function handleDirection(src) {
  var delta = (src === 'left') === inverse ? -1 : 1;
  tracker += delta;
}

31
Rory McCrossan, 13.12.2018 12:29



edit
function handleDirection(src)
{
    if (inverse) {
        (src === 'left' ? tracker-- : tracker++)
    } else {
        (src === 'left' ? tracker++ : tracker--)
    }
}

26
treyBake, 13.12.2018 12:27




edit
function handleDirection(src) {
  if (src) {
    inverse ? tracker-- : tracker++;
  } else {
    inverse ? tracker-- : tracker++;
  }
}

23
MagicLegend, 13.12.2018 12:32




edit
function getDirectionOffset(src) {
  tracker += (src === 'left' ? 1 : -1) * (inverse ? -1 : 1);
}

15
Leuronics, 13.12.2018 14:20




edit
+--------+------------+--------+
| isLeft | isInverted | Offset |
+--------+------------+--------+
| true   | true       |     -1 |
| true   | false      |      1 |
| false  | true       |      1 |
| false  | false      |     -1 |
+--------+------------+--------+
+-----------+------------+--------+
| direction | isInverted | Offset |
+-----------+------------+--------+
| left      | true       |     -1 |
| left      | false      |      1 |
| right     | true       |      1 |
| right     | false      |     -1 |
| up        | false      |      0 |
| up        | true       |      0 |
| down      | false      |      0 |
| down      | true       |      0 |
+-----------+------------+--------+
+-----------+---------------+---------+
| direction | windDirection | OffsetX |
+-----------+---------------+---------+
| left      | right         |      -1 |
| left      | up            |       1 |
| left      | down          |       1 |
| left      | left          |       2 |
| right     | up            |      -1 |
| right     | down          |      -1 |
| right     | right         |      -2 |
| right     | left          |       1 |
| up        | up            |       0 |
| up        | down          |       0 |
| up        | left          |       1 |
| up        | right         |      -1 |
| down      | up            |       0 |
| down      | down          |       0 |
| down      | left          |       1 |
| down      | right         |      -1 |
+-----------+---------------+---------+

14
vlaz, 13.12.2018 13:16




edit
// by using short circuiting
    function handleDirection(src) {
       if (src == 'left') tracker = inverse && tracker-1 || tracker +1
       else  tracker = inverse && tracker+1 || tracker -1
    }
// by using ternary operator
 function handleDirection(src) {
       if (src == 'left') tracker = inverse ? tracker-1 : tracker +1
       else  tracker = inverse ? tracker+1 : tracker -1
    }

12
Komal Bansal, 13.12.2018 12:34




edit
const LEFT = 'left'
const RIGHT = 'right'


function handleMovement(src) {
    tracker += movementIncrement(src, inverse)
}

function movementIncrement(direction, inverse) {
    if (inverse) {
        direction = inverseDirection(direction)
    }
    return LEFT ? -1 : +1
} 

function inverseDirection(direction) {
    return direction === LEFT ? RIGHT : LEFT
}

11
Anders, 13.12.2018 17:47

Add your just only code

your name:    



If you need full text click here

Some similar links Simplify an if/else statement? ↓:
execute statement after return statement in javascript

s and if/else statement

if/else statement Javascript

IF Statement Always True



This project is created for those who just need only code, wihout maaaaaaaaaaany words.