Main Page
 The gatekeeper of reality is
 quantified imagination.

Stay notified when site changes by adding your email address:

Your Email:

Bookmark and Share
Email Notification
JS Range Finder [Go Back]
Purpose
The purpose of this script is to find what number (from an array of numbers) is closest in value to a single number provided. One of the uses of this type of script could be in closely aligning an old deduction (say, from a paycheck) to the closest deduction under a different system; what comes to mind is state tax deduction changes.

How it Works
This code uses the strength of a multi-dimensional array and a simple sort function. The array "diffValues" stores three values for each row (iteration) being "column - column number", "spread - difference between array number and single number", and, "original - the original value of the array number". When an array number is entered the function "colDiffAdd()" is called. When enough array numbers have been added to the array, the function "colDiffAddSingle()" is called. That function takes the single number, applies the difference between each array number and then sorts the array diffValues[]. The first index of the array will automatically contain the "column", "spread" and "original" that is of the smallest difference (aka spread) to the single number entered.

Working Example
STEP 1: Add Numbers to Array (one at a time)

Numbers Added:

STEP 2: Enter Single Number & Tabulate


STEP 3: Result
After you have added some numbers into the array (step 1) and added a single number (step 2), the region below will tell you what number (from step 1) is closest to the single number (step 2).


The Code
<script language="javascript" type="text/javascript">
/* Numeric Range Match Finder.  Written by Joe McCormack, www.virtualsecrets.com. */
diffValues = new Array();
var resultColumn = 0, resultSpread = 0, resultOriginal = 0;
function colDiffConstructor(d1, d2) { this.column = d1; this.spread = d2; this.original = d2; }
function colDiffAdd(value) {
	diffValues[diffValues.length] = new colDiffConstructor(diffValues.length, value, value);
	if (document.getElementById("inputNumAdded")) {
	/* Show numbers added */
	var compiled = "";
	for (x = 0; x < diffValues.length; x++) {
		if (x > 0) { compiled = compiled + "," + diffValues[x].original; }
		else { compiled = diffValues[x].original; }
		}
		document.getElementById("inputNumAdded").innerHTML = compiled;
	}
	if (document.getElementById("inputNum")) { /* Clear input */ document.getElementById("inputNum").value = ""; }
}
function colDiffAddSingle(value) {
	/* Update array */
	for (x = 0; x < diffValues.length; x++) {
		var columnDiff = Math.abs(diffValues[x].spread - value);
		diffValues[x].spread = columnDiff;
	}
	/* Get closest number to value in array */
	var compiled = "";
	diffValues.sort(colDiffSortByValue);
	resultColumn = diffValues[0].column;
	resultSpread = diffValues[0].spread;
	resultOriginal = diffValues[0].original;
	if (document.getElementById("inputNumResult")) {
		compiled = "The single number entered \"" + value + "\" is closest in value to column #\"" + resultColumn + "\" with an original value of \"" + resultOriginal + "\" with a difference between the single number entered and the column value being \"" + resultSpread + "\".";
		compiled = compiled + "<br /><br /><input type=\"button\" id=\"resetBtn\" value=\"Reset\" onmouseup=\"javascript:colDiffReset();\" />";
		document.getElementById("inputNumResult").innerHTML = compiled;
	}
}
function colDiffSortByValue(a, b) {
	var x = a.spread; var y = b.spread;
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
function colDiffReset() {
	diffValues = [];
	resultColumn = 0; resultSpread = 0; resultOriginal = 0;
	if (document.getElementById("inputNum")) { document.getElementById("inputNum").value = ""; }
	if (document.getElementById("inputNumAdded")) { document.getElementById("inputNumAdded").innerHTML = ""; }
	if (document.getElementById("inputSingleNum")) { document.getElementById("inputSingleNum").value = ""; }
	if (document.getElementById("inputNumResult")) { document.getElementById("inputNumResult").innerHTML = ""; }
}
</script>
About Joe