Search code examples
jqueryvariablesif-statementshortcutshorthand-if

jquery: shorter code (if & var)


that's probably a silly question, but i have a problem not with the code not functioning - it's rather the code is too long...

everything works fine but for example is there a way to shorten the if/else ? or is it possible to track 4 different var but with a shorter code ?

var transitionState_N = '-';
var transitionState_X = '-';
var transitionState_Y = '-';
var transitionState_Z= '-';

// carousel 4way
function carousel_4way_N (n, el, m) {
    var carousel = '#carousel-4way-nav-' + n
    var carousel_content = '#carousel-4way-' + n
        if (transitionState_N == '-')
          {
          transitionState_N = '+';
          }
          else
          {
          transitionState_N = '-';
          }
        $(carousel_reset).hide();
        $(carousel).transition(
        {y: m + '=10',delay:200}
        );
        };

function carousel_4way_X (n, el, m) {
    var carousel = '#carousel-4way-nav-' + n
    var carousel_content = '#carousel-4way-' + n
        if (transitionState_X == '-')
          {
          transitionState_X = '+';
          }
          else
          {
          transitionState_X = '-';
          }
        $(carousel_reset).hide();
        $(carousel).transition(
        {y: m + '=10',delay:200}
        );
        };

function carousel_4way_Y (n, el, m) {
    var carousel = '#carousel-4way-nav-' + n
    var carousel_content = '#carousel-4way-' + n
        if (transitionState_Y == '-')
          {
          transitionState_Y = '+';
          }
          else
          {
          transitionState_Y = '-';
          }
        $(carousel_reset).hide();
        $(carousel).transition(
        {y: m + '=10',delay:200}
        );
        };

function carousel_4way_Z (n, el, m) {
    var carousel = '#carousel-4way-nav-' + n
    var carousel_content = '#carousel-4way-' + n
        if (transitionState_Z == '-')
          {
          transitionState_Z = '+';
          }
          else
          {
          transitionState_Z = '-';
          }
        $(carousel_reset).hide();
        $(carousel).transition(
        {y: m + '=10',delay:200}
        );
        };

$('#carousel-4way-nav-1').click(function(event){
    $(carousel_reset_nav).not(this).transition(
    {y:0});
    return carousel_4way_N(1, this, (transitionState == '-') ? '+' : '-');
    event.preventDefault();
});

$('#carousel-4way-nav-2').click(function(event){
    $(carousel_reset_nav).not(this).transition(
    {y:0});
    return carousel_4way_X(2, this, (transitionState == '-') ? '+' : '-');
    event.preventDefault();
});

$('#carousel-4way-nav-3').click(function(event){
    $(carousel_reset_nav).not(this).transition(
    {y:0});
    return carousel_4way_Y(3, this, (transitionState == '-') ? '+' : '-');
    event.preventDefault();
});

$('#carousel-4way-nav-4').click(function(event){
    $(carousel_reset_nav).not(this).transition(
    {y:0});
    return carousel_4way_Z(4, this, (transitionState == '-') ? '+' : '-');
    event.preventDefault();
});

Solution

  • The ternary operator makes it somewhat more readable:

    transitionState_N  = (transitionState_N == '-') ? '+' : '-';
    

    Your functions are really repetitive. Look for code you repeat often and write a function to replace it.