I've put together a little range
function in JS. I've tested it in Chrome 19, FF, and IE (7-9) and it's working well. The question I have has to do with the while
statement.
function range(from,to,step)
{
'use strict';
var sCode,eCode,result;
result = [];
step = (!step || isNaN(step) || step === 0 ? 1 : step);
sCode = (''+from).charCodeAt(0);
eCode = (''+to).charCodeAt(0);
step *= (sCode > eCode && step > 0 ? -1 : 1);
do
{
if (String.fromCharCode(sCode))
{
result.push(String.fromCharCode(sCode));
}
}while((step > 0 && eCode >= (sCode+=step)) || (step < 0 && eCode <= (sCode+=step)));
return result;
}
I remember reading a question here a while back on how JS handles Control flow constructs and logical operators. I think it had something to do with checking if an object had a certain method, and if so, using it's return value (if (event.returnValue && e.returnValue === true)
kind of stuff).
I can't seem to find that question any more, here's what I wanted to know:
while((step > 0 && eCode >= (sCode+=step)) || (step < 0 && eCode <= (sCode+=step)));
Since the function behaves as I want it to, I think I'm right in saying that, if step < 0
is false, && eCode >= (sCode+=step)
will be ignored, leaving the value of sCode
unchanged.
When the step
check is true, sCode
will be in/decremented. I've put this assignment in brackets, to make sure that the newly assigned value of sCode
will be compared to eCode
. Again, I assume that the brackets will give priority to the assignment over the logical operator.
Is this true for ALL browsers, or is it browser specific to some extent? is there a chance that this function will increment (or decrement) the value of sCode
twice in some browsers?
In this case, it's not that important (it's an easy fix to prevent any issues). But I want to know if this behaviour is inherent to JavaScript itself, or to the browser implementation.
Thanks for reading this far down. If you don't mind
A couple of other things (not important, but just wondering):
5999999999989759
which seems almost incredible, but then again I might need to brush up on my Chinese. from
is undefined, the (jslint approved) approach from.toString().charCodeAt(0);
fails, because obviously, undefined
had no toString
method, why the, does (''+from).charCodeAt(0);
return U
all the same? I thought it implicitly called the toString
method? I think I'm right in saying that, if
step < 0
is false,&& eCode >= (sCode+=step)
will be ignored, leaving the value ofsCode
unchanged
Correct. If the first operand evaluates to false, the second operand will not be evaluated.
After when the step check is true, sCode will be in/decremented. I've but this assignment in brackets, to be sure that the newly assigned value of sCode will be compared to eCode. Again, I assume that the brackets will give priority to the assignment over the logical operator.
Correct again, but the parentheses around the assignment are required, since assignment has a lower precedence than a comparison.
Is this true for ALL browsers?
Yes. I would be incredibly suprised if you find one that doesn't behave this way.
When
from
is undefined, the (jslint approved) approachfrom.toString().charCodeAt(0);
fails, because obviously, undefined had notoString
method, why the, does(''+from).charCodeAt(0);
return U all the same?
Because it concatenates the value of from
with the empty string. The value of from
is undefined
, which is coerced to a string, and you end up with the string "undefined", and the character at index 0 of that string is "u".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With