![]()
Snippets of A |
|
|
What are snippets? |
|
parseInt or (to allow decimal fractions) parseFloat:
var toNum = parseFloat(STRING);
NaN, a specific type of returned value (tantamount to false, and any conditional check would behave as if considering it such) which anyway still belongs, strangely enough, to the Number data type.
vat intoString = "" + 351;
var num=0;//or 7 or whatever number
num=!!num;
alert(num);DETAILS: In many programming languages (so called strongly typed * languages such as C * ), numbers must be declared in their signatures * as belonging within a specified range (data type * ) before being allocated: that is, before writing down the number assigned to something, you have to declare in its signature within which range such number is to fall/pertain.
byte: numbers within -128 +127
So range span is 256 (including zero). |
short: numbers within -32,768 +32,767
So range span is 65,536 (including zero). |
integer: numbers within -2,147,483,648 +2,147,483,647
So range span is 4,294,967,296 (including zero). |
long: numbers within -9223372036854775808L +9223372036854775807L
|
function cast(value, range, noRound){ //Validate: value=parseFloat(value); if(isNaN(value)){return false;}; range=parseFloat(range)||1; //Run: var division=""+(value/range); var integer=(division.indexOf(".")>=0)? division.substring(0, division.indexOf(".")):division;/*rounds outside range: useful only to recast*/ var fractional=(division.indexOf(".")>=0)? "0."+division.substring(division.indexOf(".")+1):0;/*rounds within range: useful to cast*/ var reciprocal=1/range; value=parseFloat(fractional)/reciprocal; return [ (!noRound)?Math.round(value):value, integer, range ]; /* keep this comment to reuse freely: http://www.fullposter.com/?1 */}Remove colors
false if the first input argument was not a number, or an array of three entries:
cast(300, 256); //means: translate 300 given a range of just 256
//returns:
array(44 , 1, 256)
value/range: it finds how many rounds the range falls within the scope of value (in the simplest setting the value exceeds the range).
reciprocal=1/range: do not get puzzled, this simply expresses the range in another way: as fraction of the unit, so to fit the unit of measure (number 1) of the fractional value yielded above (all fractional values, in fact, are fractions insofar as they are inferior to the unit): example if range is 3 => (0.3333333 , 0.3333333 , 0.3333333)
fractional/reciprocal: means how many times such (reciprocal) fraction (0.3333333) is found within the fractional part namely within the rounds performed inside range: we have cast.
function recast(value, integer, range){ //Validate: if(typeof(value)=="object" && value.length){ integer=value[1]; range=value[2]; value=value[0]; }; value=parseFloat(value); integer=Math.round(parseFloat(integer)); range=parseFloat(range); if(isNaN(value) || isNaN(integer) || isNaN(range)){return false;}; //Run: return (range*integer/*rounds made*/)+value; /* keep this comment to reuse freely: http://www.fullposter.com/?1 */}Remove colors
false if the input arguments weren't numbers, or a number.
//prepare inputs: var alpha="abcdefghijklmnopqrstuvwxyz"; /*bigger range from which to get...*/ var letterToCast="s"; /*...an item to cast into the...*/ var rangeTocast="abcdx"; /*...smaller range.*/ //run: var foo=cast( alpha.indexOf(letterToCast), rangeTocast.length ); //inspect returned values: alert(foo); /*that prints: (3, 3, 5): letter s in the range "abcdefghijklmnopqrstuvwxyz" cast into an alphabet range as "abcdx" or whatever: values are immaterial once they are univocally identified by numerical indexes*/ alert( rangeTocast.charAt(foo[0]) ); /*from the index to the string: returns 'd': letter 's' is cast in letter 'd' in the given settings*/ //recast: alert( alpha.charAt( recast(foo[0], foo[1], foo[2]) ) );Remove colors