Shane Chism

Summation Example


Description


This script shows how a mathematical operation, in this case a summation, can be applied in terms of code. In this instance we've got sum{i=0}{n}{i^2}, where n is the input parameter.

You can see the mathematics at WolframAlpha.

See it in Action


Specify the "N" value:

Code

squareToN.js
View Plain   Download Code
function SquareToN(){

	// Assuming nValue is a form field containing the "n" value
	// Example: <input type="text" id="nValue" value="5" />
	var n = document.getElementById( 'nValue' ).value;
	var x = 0;
	for( i = 0; i <= n; i++ )
		x = x + ( i * i );
		
	alert( "Summation of (i^2) from i = 0 to n = " + n + " is " + x );

}


 
JavaScript
NOVEMBER 23 2010
Download
67 DOWNLOADS