10 Minute Tutorial over Logarithms with a Mix of Javascript


Summary:
This tutorial will provide a simple overview of logarithms.

What are Logarithms?
Logarithms are used to determine the exponent needed to receive a certain value with a particular base.

Example: Log 100 = 2. Since 10^2 = 100.

Here’s a short video explaining logarithms more in-depth with a practical example.

The most common bases used for logarithms are base 10 and E. With base E logarithms normally referred to as the natural logarithm.
In Javascript, the function Math.log returns the natural logarithm of the argument instead of a base 10 logarithm. This can cause some confusion for those unaware of this fact.

Math.log( 100 ) == 2 // returns false
Math.log( 100 ) // returns 4.605170185988092

So how can one use a different base other than E? Well, it simple. All you have to do is take the log of the value that you want, then divide that by the log of the desired based.
Like so.

Math.log( x ) / Math.log( desiredBase );

Here’s a user defined function that does the same operation.

/**
* @function Math.logx
* @purpose: To provide the logarithm for any base desired. Default base is 10.
* @returns a number.
*/
Math.logx = function(x,base) {
    return (Math.log(x)) / (Math.log(base | 10 ));
}

And now, we can calculate log 10 as 2 instead of another number.

Math.log( 100 ) == 2    // returns false
Math.logx( 100 ) == 2   // returns true

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube

Code of the day: Javascript convert bytes to KB, MB, GB, etc

Note: This uses the IEC standard. That means 1 KB = 1024, not 1000 like the SI standard.

/**
* @function: getBytesWithUnit()
* @purpose: Converts bytes to the most simplified unit.
* @param: (number) bytes, the amount of bytes
* @returns: (string)
*/
var getBytesWithUnit = function( bytes ){
	if( isNaN( bytes ) ){ return; }
	var units = [ ' bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB' ];
	var amountOf2s = Math.floor( Math.log( +bytes )/Math.log(2) );
	if( amountOf2s < 1 ){
		amountOf2s = 0;
	}
	var i = Math.floor( amountOf2s / 10 );
	bytes = +bytes / Math.pow( 2, 10*i );
 
	// Rounds to 3 decimals places.
        if( bytes.toString().length > bytes.toFixed(3).toString().length ){
            bytes = bytes.toFixed(3);
        }
	return bytes + units[i];
};

Output:

console.log( getBytesWithUnit( ) );			// returns undefined.
console.log( getBytesWithUnit( 'non a number.' ) );	// returns undefined.
console.log( getBytesWithUnit( '123' ));		// returns '123 bytes'.
console.log( getBytesWithUnit( 1024 ));		// returns '1 KB'.
console.log( getBytesWithUnit( (1024 * 1024) + 1024 )); // returns '1.001 MB'.
console.log( getBytesWithUnit( 1024 * 1024 * 1024 )); // returns '1 GB'.
console.log( getBytesWithUnit( 1024 * 1024 * 64 )); // returns '64 MB'.

Update
I updated the script to support both the SI and IEC standard.
Check it out here http://bateru.com/news/2012/03/code-of-the-day-converts-bytes-to-unit/

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube

jQuizMe Example: Simple Math Quiz

Here’s an example of jQuizMe.
Goal: Make a quiz for Addition, Subtraction and Multiplication for two digit numbers.

Steps:

  1. Make a random number generator.
  2. Make a function to form a question and answers.
  3. Form the quiz.
  4. Call jQuizMe

Step 1:
I wrote a random number generator in javascript here.

1
2
3
4
var getRandomNum = function( i, decLen ){
	var powOf10s = Math.pow( 10, decLen || 0 );
	return i ?  Math.floor( powOf10s * Math.random() * i ) / powOf10s : Math.random();
};

Or.. you could just use Math.floor( Math.random() * number ).

Step 2 & 3: (Due to time constraints I combined step 2 and 3.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
var link = "http://bateru.com/news/2011/04/learn-sig-figs-in-no-time/";
var ops = operations = [ " + ", " - ", " * ", " / " ];
var precision = 2;
 
var arrOfRandomNum = function( arrLength, max, decLen ){
	if( arrLength < 0 ){ return false; }
	var arr = [], i = arrLength;
	while( i-- ){
		arr[ i ] = getRandomNum( max, decLen );
	}
	return arr;
};
var makeQuiz = function(){
	var quiz = { "fill":[] };
	var question = '', nums = [];
	var i = questionLength = 10;
	while( i-- ){
		nums = arrOfRandomNum( 2, 19 );
		question = nums.join( ops[ getRandomNum( ops.length ) ] );
		quiz.fill.push({
			ques: question,
			ans: eval( question ).toPrecision(precision)
		});
	}
	return quiz;
}

Step 4:

1
2
3
$( "#quiz" ).jQuizMe( makeQuiz(), {
	intro: ops.join(',') + " quiz.<br>Note: Your answer must have " + precision + " <a href='"+link+"'>sig figs</a>."
});

Here’s the working demo.

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube