jQuizMe Example: Simple Math Quiz


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

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

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

Code of the day: C, Get the Digits from a Number


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

Today’s Code of the Day is about how to find the digit of a number at a given position. I decided to go with the C / C++ programming languages to time, but you can easily translate the algorithms to other languages.

C Code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <math.h>
 
// Function: getDigitFromNum returns a digit at a given index of a integer.
// Goes from right to left with the starting index of 0.
int getDigitFromNum( int num, int digit ){
    num /= pow( 10, digit );
    return num % 10;
}
// Function: getDigitFromDec returns a digit at a given index of a double.
// Goes from left to right with the starting index of 0.
int getDigitFromDec( double dec, int digit ){
    dec *= pow( 10, digit );
    return (int)dec % 10;
}
// Function: getDigitFromNum returns the decimal values of a double.
double getDecimals( double dec ){
    return dec - (int)dec;
}

Example:
Input File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Programmed by Larry Battle, bateru.com/news
// Date: April 26, 2011
// Purpose: To demonstrate how to get a digit by a position index.
#include <stdio.h>
#include <math.h>
 
int getDigitFromNum( int num, int digit ){
    num /= pow( 10, digit );
    return num % 10;
}
int getDigitFromDec( double dec, int digit ){
    dec *= pow( 10, digit );
    return ((int)dec) % 10;
}
double getDecimals( double dec ){
    return dec - (int)dec;
}
 
void main(){
    double dec = 1234.56789;
    printf( "\n%d", getDigitFromNum( (int)dec, 0 ));
    printf( "\n%d", getDigitFromDec( dec, 2 ));
    printf( "\n%f", getDecimals( dec ));
}

Output:

1
2
3
4
6
0.567890
Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

Javascript Standard Deviation, Variance, Average functions.


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

Hey everyone,
I just installed WP-Syntax for the WordPress and I want to test it out by posting javascript code for finding Standard Deviation, Variance, Average among numbers.

Without WP-Syntax

var str = 'This is a string.';
console.log( str ); //Boring!

With WP-Syntax

var str = 'hello world. Here I am!';
console.log( str ); //Nice colors!!

Without more delay, here are the Statistics functions.

Code

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
27
28
29
30
31
32
33
34
35
36
// Programmer: Larry Battle 
// Date: Mar 06, 2011
// Purpose: Calculate standard deviation, variance, and average among an array of numbers.
var isArray = function (obj) {
	return Object.prototype.toString.call(obj) === "[object Array]";
},
getNumWithSetDec = function( num, numOfDec ){
	var pow10s = Math.pow( 10, numOfDec || 0 );
	return ( numOfDec ) ? Math.round( pow10s * num ) / pow10s : num;
},
getAverageFromNumArr = function( numArr, numOfDec ){
	if( !isArray( numArr ) ){ return false;	}
	var i = numArr.length, 
		sum = 0;
	while( i-- ){
		sum += numArr[ i ];
	}
	return getNumWithSetDec( (sum / numArr.length ), numOfDec );
},
getVariance = function( numArr, numOfDec ){
	if( !isArray(numArr) ){ return false; }
	var avg = getAverageFromNumArr( numArr, numOfDec ), 
		i = numArr.length,
		v = 0;
 
	while( i-- ){
		v += Math.pow( (numArr[ i ] - avg), 2 );
	}
	v /= numArr.length;
	return getNumWithSetDec( v, numOfDec );
},
getStandardDeviation = function( numArr, numOfDec ){
	if( !isArray(numArr) ){ return false; }
	var stdDev = Math.sqrt( getVariance( numArr, numOfDec ) );
	return getNumWithSetDec( stdDev, numOfDec );
};

Usage

Testing with numbers from wiki

1
2
3
4
5
6
var arrOfNums = [ 2,4,4,4,5,5,7,9 ],
	precision = 4;
 
getAverageFromNumArr( arrOfNums, precision );	//returns 5
getVariance( arrOfNums, precision );	// returns 4
getStandardDeviation( arrOfNums, precision ); //returns 2

Enjoy! 🙂
Update
For those wanting more statistical functions, use jStat : a JavaScript statistical library. by John Resig.

jStat is a statistical library written in JavaScript that allows you to perform advanced statistical operations without the need of a dedicated statistical language (i.e. MATLAB or R).

Update 2
Please read this for more information “http://easycalculation.com/statistics/learn-standard-deviation.php”

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube