[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Javascripts help with interpretation

JT

6/30/2014 9:24:00 PM

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?
3 Answers

Andrew Poulos

6/30/2014 9:37:00 PM

0

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_Op...

Andrew Poulos

JT

6/30/2014 9:51:00 PM

0

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_Op...
>
>
>
> 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>

Tim Slattery

6/30/2014 10:05:00 PM

0

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