Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

 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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);   // πŸ˜„
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading