MysteryPile Developer

MP Numerology Calculator - JS Iteration

Translate words into Destiny numbers and dates into Life numbers using an HTML web form and JavaScript. The calculator reduces input from a text field to a single digit number using numerology process. Installation is easy and the JavaScript iteration enables values to be updated inline without having to refresh the page.

JavaScript

Form action triggers a numerology calculator script which begins with setting number results to zero as the clean slate. Then, input values are read from text fields and passed to the printing function.

function numerologyValues() {
	var destinyName = 0;
	var lifeDate = 0;
	destinyName = document.getElementById("inputNumber").value;
	lifeDate = document.getElementById("inputLife").value;
	printNumerologyValues(destinyName, lifeDate);
}

To ensure printing fields do not display tags when echoed, the strip_tags function by Locutus is used to remove tags, comments, and code. The function is a translation of PHP strip_tags() mastered to work with JavaScript.

function strip_tags (input, allowed) { 
  allowed = (((allowed || '') + '').toLowerCase().match(/<[a-z][a-z0-9]*>/g)
 || []).join('');
  var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi;
  var commentsAndPhpTags = /|<\?(?:php)?[\s\S]*?\?>/gi;
  return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
    return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
  })
}

In numerology, the result number is always a single digit attained by adding each of the digits for values greater than nine. The reduceNumber function applies this numerology formula to an input and returns a single digit.

function reduceNumber(innum) {
	while(innum > 9) { 
		var tempSubName = innum.toString().split("");
		var subNameValue = 0;
		for (x = 0, y = tempSubName.length; x < y; x++) 
		{ subNameValue = subNameValue + parseInt(tempSubName[x]); }
		innum = subNameValue;	
	}
	return innum;
}

Printing results first involves validation of text field inputs, building the display, and handling some exceptions before values are fetched. Input from the Life field requires numbers only while the Destiny field requires letters and spaces only. Result values are returned when those conditions are met, otherwise an error message is passed to display.

function printNumerologyValues(edn, eld) { 

	/* build display */
	var errorMessageLife = "Accepts numbers above zero";
	var errorMessageName = "Accepts letters and spaces";
	var displayEdn, displayEld;
	displayEdn = strip_tags(edn.toString());
	displayEld = strip_tags(eld.toString());
	if (displayEdn.length > 21) 
	{ displayEdn = displayEdn.substring(0, 21).concat("~"); }
	if (displayEld.length > 21) 
	{ displayEld = displayEld.substring(0, 21).concat("~"); }

	/* print inputs */
	document.getElementById("enteredLifeDate").innerHTML = displayEld;
	document.getElementById("enteredDestinyName").innerHTML = displayEdn;

 	/* validate */
	var dnCalc, lnCalc = 0;
	var tempDn = strip_tags(edn.replace(/\s+/g, ''));
	var templN = strip_tags(eld.replace(/\s+/g, ''));
	var lnRegEx = templN.match(/[^0-9]+/);
	var dnRegEx = tempDn.match(/[^a-zA-Z ]+/);
	if (dnRegEx != null ) { dnCalc = -1; }
	else { 	dnCalc = evaluateDestinyNumber(tempDn);}
	if (lnRegEx != null ) { lnCalc = -1; }
	else {	lnCalc = evaluateLifeNumber(templN); }

	/* results */
	if (dnCalc < 1) {
	document.getElementById("destinyResult").innerHTML = errorMessageName;} 
	else { 
	document.getElementById("destinyResult").innerHTML = dnCalc.toString();}
	if (lnCalc < 1) {
	document.getElementById("lifeResult").innerHTML = errorMessageLife;}
	else { 
	document.getElementById("lifeResult").innerHTML = lnCalc.toString();}
}

The life number function works in conjunction with the reduce number function to recursively add multiple digits of the input together and return a single digit result. Text field values are treated as a string and split into an array for addition.

function evaluateLifeNumber(elnc) {
	var lifeNumber = 0;
	var tempLifeNumber = elnc.toString().trim().split("");	
	for (p = 0, q = tempLifeNumber.length; p < q; p++) 
	{lifeNumber = lifeNumber + parseInt(tempLifeNumber[p]);}
	lifeNumber = reduceNumber(lifeNumber);
	return lifeNumber;
}

Calculating the Destiny number is a little more involved than deriving a Life number since each letter from the input must be equated to its numerology counterpart. Nested loops evaluate each word from the input field by adding the value for each character together. The temporary result is passed to the reduce number function each iteration to then return a single digit result.

function evaluateDestinyNumber(ednc) {
	var destinyNumber = 0;
	var inputtoArr = ednc.toString().split(" "); 
	var countNames = inputtoArr.length;
	var nameSum = [countNames-1];
	var totalNameValue =[];
	for(i = 0, c = countNames; i < c; i++) {
		var currentName = inputtoArr[i];
		var currentNameValue = 0;
		var tempName = currentName.toLowerCase();
		var tempNameArr = tempName.split(""); 
		for (j = 0, d = tempNameArr.length; j < d; j++) {
			if(tempNameArr[j] == "a" || tempNameArr[j] == "j" || 
			tempNameArr[j] == "s") { nameSum[i] = nameSum[i] + 1; }
			if(tempNameArr[j] == "b" || tempNameArr[j] == "k" || 
			tempNameArr[j] == "t") { nameSum[i] = nameSum[i] + 2; }
			if(tempNameArr[j] == "c" || tempNameArr[j] == "l" || 
			tempNameArr[j] == "u") { nameSum[i] = nameSum[i] + 3; }
			if(tempNameArr[j] == "d" || tempNameArr[j] == "m" || 
			tempNameArr[j] == "v") { nameSum[i] = nameSum[i] + 4; }
			if(tempNameArr[j] == "e" || tempNameArr[j] == "n" || 
			tempNameArr[j] == "w") { nameSum[i] = nameSum[i] + 5; }
			if(tempNameArr[j] == "f" || tempNameArr[j] == "o" || 
			tempNameArr[j] == "x") { nameSum[i] = nameSum[i] + 6; }
			if(tempNameArr[j] == "g" || tempNameArr[j] == "p" || 
			tempNameArr[j] == "y") { nameSum[i] = nameSum[i] + 7; }
			if(tempNameArr[j] == "h" || tempNameArr[j] == "q" || 
			tempNameArr[j] == "z") { nameSum[i] = nameSum[i] + 8; }
			if(tempNameArr[j] == "i" || tempNameArr[j] == "r" ) 
			{ nameSum[i] = nameSum[i] + 9; }
		}
		var sumCurrentName = nameSum[i].toString().split("");
		for (k = 0, e = sumCurrentName.length; k < e; k++) {
		currentNameValue = currentNameValue + parseInt(sumCurrentName[k]);
		}
		totalNameValue[i] = reduceNumber(currentNameValue);
	}
	for (m = 0, g = totalNameValue.length; m < g; m++) 
		{destinyNumber = destinyNumber + parseInt(totalNameValue[m]);}
	return destinyNumber;
}

HTML

To display the numerology calculator on a web page, include the script file and use inline form text fields to update elements with calculated results. A button form element triggers the script with OnClick to calculate and display results without refreshing the page.

<script async src="mpnc.js" type="text/javascript"></script>
<form id="numerologyCalculator">
	Destiny Name: <input type="text" id="inputNumber"> 
	<span id="enteredDestinyName"> </span> = 
	<span id="destinyResult"> </span>
	Life Date: <input type="text" id="inputLife"> 
	<span id="enteredLifeDate"> </span> = 
	<span id="lifeResult"> </span>
	<button type="button" onClick="numerologyValues();">Calculate</button>
</form>

Download source code - mpnc.js

Live page - Numerology Calculator