Function to determine the direction of an arrow I have a function in a project that I feel could be written better. The purpose of the function is to return the direction of an arrow based on two parameters.


positiveDirection: The direction for a positive result. The values could be increasing or decreasing.
changeType: Whether the result was positive, negative or no-change.


I guess it's the if statement that is bothering me the most; it looks like it could be reduced based on some logic gate that I don't know of.



  getArrowDirection = (positiveDirection, changeType) =&gt; {

    let direction = null

    if(changeType === 'no-change') {
      return direction
    }

    if(
        (changeType === 'positive' &amp;&amp; positiveDirection === 'increasing') ||
        (changeType === 'negative' &amp;&amp; positiveDirection === 'decreasing')
      ) {
      direction = 'up-arrow'
    } else if(
        (changeType === 'positive' &amp;&amp; positiveDirection === 'decreasing') ||
        (changeType === 'negative' &amp;&amp; positiveDirection === 'increasing')
      ) {
      direction = 'down-arrow'
    }

    return direction
  }
The test for if(changeType === 'no-change') { is redundant and not needed as it will fall through if the other tests fail and return null anyways.

You could also break the statement up returning the result as needed and falling through if they fail for null.

const getArrowDirection = (dir, type) =&gt; {
    if (type === "positive") {
        if (dir === "increasing") { return "up-arrow" }
        if (dir === "decreasing") { return "down-arrow" }
    }else if (type === "negative") {
        if (dir === "increasing") { return "down-arrow" }
        if (dir === "decreasing") { return "up-arrow" }
    }
    return null;
}


Assuming that you have given all possible values you can make assumptions and reduce the code further 

const getArrowDirection = (dir, type) =&gt; {
    if (type !== 'no-change') {
        if (type === "positive") { return dir === "increasing" ? "up-arrow" : "down-arrow" }
        return dir === "increasing" ? "down-arrow" : "up-arrow";
    }
    return null; // returning null is not the best. Returning undefined would
                 // be better and would not need this line
}


You can use an object as a lookup using the combined strings.

const getArrowDirection = (() =&gt; {
    const directions = {
        positiveincreasing: "up-arrow",
        negativedecreasing: "up-arrow",
        positivedecreasing: "down-arrow",
        negativeincreasing: "down-arrow",
    };
    return (dir, type) =&gt; directions[type + dir] ? directions[type + dir] : null;
})();