Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #25145 > unrolled thread

Javascripts help with interpretation

Started byjonas.thornvall@gmail.com
First post2014-06-30 14:23 -0700
Last post2014-06-30 18:04 -0400
Articles 4 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  Javascripts help with interpretation jonas.thornvall@gmail.com - 2014-06-30 14:23 -0700
    Re: Javascripts help with interpretation Andrew Poulos <ap_prog@hotmail.com> - 2014-07-01 07:36 +1000
      Re: Javascripts help with interpretation jonas.thornvall@gmail.com - 2014-06-30 14:51 -0700
    Re: Javascripts help with interpretation Tim Slattery <tim@risingdove.com> - 2014-06-30 18:04 -0400

#25145 — Javascripts help with interpretation

Fromjonas.thornvall@gmail.com
Date2014-06-30 14:23 -0700
SubjectJavascripts help with interpretation
Message-ID<321d3d42-d0a2-4eab-9aa1-559aac68d20c@googlegroups.com>
Sometimes when i read code i realise my programming skils far to ancient.
 
return ((N<0) ? "-"+HexN : HexN);


This function return is a string but what does ((N<0) ? actually mean there is no if. And what about "-"+HexN : HexN); 

The first could be string building but what is : (colon) supposed to mean is it append?

[toc] | [next] | [standalone]


#25146

FromAndrew Poulos <ap_prog@hotmail.com>
Date2014-07-01 07:36 +1000
Message-ID<voCdncMEf9yVRCzOnZ2dnUVZ_sGdnZ2d@westnet.com.au>
In reply to#25145
On 1/07/2014 7:23 AM, jonas.thornvall@gmail.com wrote:
> Sometimes when i read code i realise my programming skils far to ancient.
>
> return ((N<0) ? "-"+HexN : HexN);
>
>
> This function return is a string but what does ((N<0) ? actually mean there is no if. And what about "-"+HexN : HexN);
>
> The first could be string building but what is : (colon) supposed to mean is it append?

Sorry for sending this to you personal email - I pressed Reply instead 
of Followup :-(

Maybe this link will help
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator>

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


#25147

Fromjonas.thornvall@gmail.com
Date2014-06-30 14:51 -0700
Message-ID<0ad86ec5-fb8d-409f-a393-e823717687a1@googlegroups.com>
In reply to#25146
Den måndagen den 30:e juni 2014 kl. 23:36:45 UTC+2 skrev Andrew Poulos:
> On 1/07/2014 7:23 AM, jonas.thornvall@gmail.com wrote:
> 
> > Sometimes when i read code i realise my programming skils far to ancient.
> 
> >
> 
> > return ((N<0) ? "-"+HexN : HexN);
> 
> >
> 
> >
> 
> > This function return is a string but what does ((N<0) ? actually mean there is no if. And what about "-"+HexN : HexN);
> 
> >
> 
> > The first could be string building but what is : (colon) supposed to mean is it append?
> 
> 
> 
> Sorry for sending this to you personal email - I pressed Reply instead 
> 
> of Followup :-(
> 
> 
> 
> Maybe this link will help
> 
> <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator>
> 
> 
> 
> Andrew Poulos

Well i found some very nice written code for a generic base changer, probelm is i really do not get the last line. Is he just reversing sign, if the string is negative?

<script type="text/javascript">

function toRadix(N,radix) {
 var HexN="", Q=Math.floor(Math.abs(N)), R;
 while (true) {
  R=Q%radix;
  HexN = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(R)
       + HexN;
  Q=(Q-R)/radix; 
  if (Q==0) break;
 }
 return ((N<0) ? "-"+HexN : HexN);
}

out=toRadix(65453,11)
document.write(out);
</script>

[toc] | [prev] | [next] | [standalone]


#25148

FromTim Slattery <tim@risingdove.com>
Date2014-06-30 18:04 -0400
Message-ID<0kn3r95jms31nkpn2vuv14plgk02cm3amn@4ax.com>
In reply to#25145
jonas.thornvall@gmail.com wrote:

>Sometimes when i read code i realise my programming skils far to ancient.
> 
>return ((N<0) ? "-"+HexN : HexN);
>
>
>This function return is a string but what does ((N<0) ? actually mean 
>there is no if. And what about "-"+HexN : HexN); 

It's the ternary operator. The first part is delimited by the "?", the
second by the ":". It means: If N is less than zero, return "-"
concatenated to HexN, else return HexN unchanged.

-- 
Tim Slattery
tim <at> risingdove <dot> com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web