how to shorten repetitive if else statement in single function in react js

Viewed 30
 let e2 = count2 > 2 ? "" : count2 > 5 ? "" : count2 > 8 ? "" : "";
  let e3 = count3 > 2 ? "" : count3 > 5 ? "" : count3 > 8 ? "" : "";
  let e4 = count4 > 2 ? "" : count4 > 5 ? "" : count4> 8 ? "" : "";

In this code user have three input fields where he gives the value of count2,count3,count4 in NUMBER. According to the particular value provided by the user our alogritham returns an emoji.That emoji is based upon how large the value has been given as explained in upper coding.BUT THE THIS ALOGRITHAM ONLY RETURNS THE FIRST AND LAST EMOJI AND NEGLECTS THE EMOJIS IN BETWEEN

1 Answers

When you want to repeat some tasks, just use functions.

function countInEmoji(count) {
    if (count === 0) {
        return "";
    } else if (count < 4) {
        return "";
    } else if (count < 6) {
        return "";
    } else {
        return "";
    }
}

count1 = countInEmoji(2);   // 
count2 = countInEmoji(13);  // 
count3 = countInEmoji(6);   // 
Related