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 understand Ternary JavaScript expression?

I’m bad with JS at now, especially with the operator "?"

And I’m trying to understand the following code.

Maybe it could be more friendly ?

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

So, if I don’t want to use this operator, how can it be looks.

JavaScript code:

function(t) {
       for (var e, r = t.length, n = "", i = 0, s = 0, a = 0; i < r; )
           (s = t.charCodeAt(i)) < 128 ? (n += String.fromCharCode(s),
           i++) : s > 191 && s < 224 ? (a = t.charCodeAt(i + 1),
           n += String.fromCharCode((31 & s) << 6 | 63 & a),
           i += 2) : (a = t.charCodeAt(i + 1),

           e = t.charCodeAt(i + 2),
           n += String.fromCharCode((15 & s) << 12 | (63 & a) << 6 | 63 & e),
           i += 3);
       return n
   }

>Solution :

It seems like this is same as:

function(t) {
       for (var e, r = t.length, n = "", i = 0, s = 0, a = 0; i < r; )
           if((s = t.charCodeAt(i)) < 128) {
              n += String.fromCharCode(s);
              i++;
           } else if(s > 191 && s < 224) { 
              a = t.charCodeAt(i + 1);
              n += String.fromCharCode((31 & s) << 6 | 63 & a);
              i += 2;
           } else {
              a = t.charCodeAt(i + 1);
              e = t.charCodeAt(i + 2);
              n += String.fromCharCode((15 & s) << 12 | (63 & a) << 6 | 
              63 & e);
              i += 3;
           }
       return n
   }
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