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):
- what is the max charCode in JavaScript? A quick look on google didn’t tell me, and testing in the JS console lead me to believe this was
5999999999989759which seems almost incredible, but then again I might need to brush up on my Chinese. - When
fromis undefined, the (jslint approved) approachfrom.toString().charCodeAt(0);fails, because obviously,undefinedhad notoStringmethod, why the, does(''+from).charCodeAt(0);returnUall the same? I thought it implicitly called thetoStringmethod?
Correct. If the first operand evaluates to false, the second operand will not be evaluated.
Correct again, but the parentheses around the assignment are required, since assignment has a lower precedence than a comparison.
Yes. I would be incredibly suprised if you find one that doesn’t behave this way.
Because it concatenates the value of
fromwith the empty string. The value offromisundefined, 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”.