Yves' functions, methods and other utilities library.



These are all cut-and-pastable. Just save them as a text, or JS file.

Return to Home
                    function numtransform(number){
                        //This function turns large integers into easier to read numbers, on the # ### model
                        //Not designed for use with floating numbers. Returns a string.
                        //Will also transform strings in the same way, "banana"="ban ana".
                        let number$="";
                        if (number<0){number$="-"+numtransform(-number);}
                        let tnumber$=number.toString();
                        let numlength=tnumber$.length;let init=numlength%3;
                        number$=tnumber$.substring(0,init);
                        if (number==number$){return number$}else{
                            let unumber$=tnumber$.slice(number$.length);
                            for (let aa=0;aa<=unumber$.length/3;aa++){
                            number$+=" "+unumber$.slice((aa*3),(aa+1)*3)
                                    }
                                }
                            return number$;
                            }
        


        function approximates(number,number2,epsilon=0.01){
            //This functions compares two numbers to determine if they are "close"
            //epsilon represents an acceptability range (of 1 percent by default)
            //If the two numbers are within the acceptability range, the function returns true.
            let answer=false;
            let acceptableError=number*epsilon;
            let minAnswer=number-acceptableError;
            let maxAnswer=number+acceptableError;
            if (minAnswer<=number2 && maxAnswer>=number2){answer=true;}
            if (number<0&&minAnswer>=number2&&maxAnswer<=number2){answer=true;}
            return answer;
            }
    


The following is a random number generator.

For those familiar with Dungeons and Dragons, it takes input as per the D&D standard.

That is to say that throwDice(3 6 1) corresponds to D&Ds 3d6+1; three random numbers from 1 to 6, plus 1

throwDice(1 10) simply returns a random number from 1 to 10. A bonus of -1 makes it 0 to 9. etc...

            function throwDice(dice,type,bonus=0){
                //this function generates a random number based on 2 or 3 inputs.
                //first two inputs must be positive integers, otherwise the result will be nonsensical.
                //third input may be a negative integer, or a float and is optional.
                //result is the sum of (dice) random numbers of (1 to type) +(bonus)
                let aa=0;
                for (let bb=1;bb<=dice;bb++){aa+=Math.ceil(Math.random()*type)}
                aa+=bonus;
                return aa;}
    


The following three functions make up two utilities to transform a date into a number, and vice-versa.

Note that the isLeap function is required by the other two functions.

            function dateToYear(year,month,day=1,hour=0,minute=0){
                //This function is meant to express a fraction of a year as a date
                //For instance, 1945/07/02 at midnight transforms to 1945.5
                //Useful if you need to express a date as a fraction of a year.
                //You may also specify the hours and minutes, which are 0 by default. (i.e. 00:00)
                let response=parseInt(year);
                if (isLeap(year)){var fe=29}else{var fe=28};
                let months=[31,fe,31,30,31,30,31,31,30,31,30,31];
                if (fe==28){var dayz=365}else{var dayz=366};
                let total=0;
                for (let aa=0;aa<=month-2;aa++){total+=months[aa];};
                total+=parseInt(day)-1;
                response=response+(total/dayz)+(hour/dayz/24)+(minute/dayz/24/60);
                return response;}
        
            function yearToDate(year){
                //This function is the opposite of the previous function.
                //Will transform a year, provided as a fraction into long form.
                let ye=Math.floor(year);
                let rest=year-ye;
                if (isLeap(ye)){var fe=29}else{var fe=28};
                let months=[31,fe,31,30,31,30,31,31,30,31,30,31];
                if (fe=28){var dayz=365}else{var dayz=366};
                //'var' is generally not recommended, but I use it here for the specific
                //reason I need to declare a variable that will be read beyond its immediate scope.
                //ternary operators, or pre-declaration of the variables are ways around this.
                rest=rest*dayz;
                //The strategy used to come up with the number is to take away a whole number,
                //representing a unit of measurement (ex. 2020.4-2020=.4).
                //then multiplying the remainder by the number of times the previous unit is contained in the first one.
                //(ex .4*365 days in a year=146th day of the year)
                days=Math.floor(rest);
                let aa=0;
                while (months[aa]<=rest){rest=rest-months[aa];aa++}
                let month=aa+1;
                let day=Math.floor(rest)+1;
                rest=rest-day+1;
                rest=rest*24;
                let hour=Math.floor(rest);rest=rest-hour;
                rest=rest*60;
                let minute=Math.floor(rest);rest=rest-minute;
                second=Math.floor(rest*60);
                let response$=(ye+"/"+month+"/"+day+" "+hour+": "+minute+": "+second);
                return response$;}
  
            function isLeap(year){
                //small function which simply answers the question: Is this a leap year?
                //Used by the two functions above (They will not work unless you include this small function as well)
                //returns true if conditions are meant for a leap year, false otherwise. Takes a number as input, returns Boolean.
                let aa=false;
                if (year%4==0){aa=true}
                if (year%100==0){aa=false}
                if (year%400==0){aa=true}
                return aa;}
    


Download full library (Js)