Skip to content

Instantly share code, notes, and snippets.

@bell345
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bell345/48ee54a96dfbcee82405 to your computer and use it in GitHub Desktop.
Save bell345/48ee54a96dfbcee82405 to your computer and use it in GitHub Desktop.
My "math" library from when I first started coding.
function doMath()
{
var mathCond = document.getElementById("mcond").value;
if ((mathCond == "x") || (mathCond == "*") || (mathCond == "times") || (mathCond == "multiply"))
var output = multiplyMath();
else if ((mathCond == "+") || (mathCond == "add") || (mathCond == "plus"))
var output = addMath();
else if ((mathCond == "-") || (mathCond == "subtract") || (mathCond == "minus"))
var output = subtractMath();
else if ((mathCond == "/") || (mathCond == "divide"))
var output = divideMath();
else if ((mathCond == "rtc") || (mathCond == "radiustocircumference"))
var output = rtocMath();
else if ((mathCond == "dtc") || (mathCond == "diametertocircumference"))
var output = dtocMath();
else if ((mathCond == "sqrt") || (mathCond == "square root"))
var output = sqrtMath();
else if ((mathCond == "^") || (mathCond == "power") || (mathCond == "pwr") || (mathCond == "pow"))
var output = pwrMath();
else if ((mathCond == "dectodeg") || (mathCond == "decimaltodegree"))
var output = dectodegMath();
else if ((mathCond == "degtodec") || (mathCond == "degreetodecimal"))
var output = degtodecMath();
else if ((mathCond == "sine") || (mathCond == "sin"))
var output = sinMath();
else if ((mathCond == "cosine") || (mathCond == "cos"))
var output = cosMath();
else if ((mathCond == "tangent") || (mathCond == "tan"))
var output = tanMath();
else if ((mathCond == "coin") || (mathCond == "coin flip"))
var output = coinRand();
else if ((mathCond == "floatrand") || (mathCond == "floating point") || (mathCond == "0-1"))
var output = floatRand();
else if ((mathCond == "percent") || (mathCond == "out of 100") || (mathCond == "%"))
var output = percentRand();
else if ((mathCond == "integer") || (mathCond == "random integer") || (mathCond == "randint"))
var output = intRand();
else
alert("The operator you have chosen is not valid. All valid operators are inside the drop-down menu.");
var mout = document.getElementById("mout");
mout.innerHTML = "The result is: <br> <input type='text' class='mainf' value='" + output + "' id='mcondbox'>";
}
function multiplyMath()
{
var input1 = document.getElementById("minput1").value;
var input2 = document.getElementById("minput2").value;
var input3 = document.getElementById("minput3").value;
var input4 = document.getElementById("minput4").value;
var input5 = document.getElementById("minput5").value;
if (input2 == "")
input2 = 1;
if (input3 == "")
input3 = 1;
if (input4 == "")
input4 = 1;
if (input5 == "")
input5 = 1;
var output = input1 * input2 * input3 * input4 * input5;
return output;
}
function addMath() {
var input1 = Number(document.getElementById("minput1").value);
var input2 = Number(document.getElementById("minput2").value);
var input3 = Number(document.getElementById("minput3").value);
var input4 = Number(document.getElementById("minput4").value);
var input5 = Number(document.getElementById("minput5").value);
if (input2 == "")
input2 = 0;
if (input3 == "")
input3 = 0;
if (input4 == "")
input4 = 0;
if (input5 == "")
input5 = 0;
var output = input1 + input2 + input3 + input4 + input5;
return output;
}
function divideMath() {
var input1 = document.getElementById("minput1").value;
var input2 = document.getElementById("minput2").value;
var input3 = document.getElementById("minput3").value;
var input4 = document.getElementById("minput4").value;
var input5 = document.getElementById("minput5").value;
if (input2 == "")
input2 = 1;
if (input3 == "")
input3 = 1;
if (input4 == "")
input4 = 1;
if (input5 == "")
input5 = 1;
var output = input1 / input2 / input3 / input4 / input5;
if (output == "Infinity") {
alert("You divided by zero, didn't you? Don't worry, it won't break.");
output = "Infinity!!!!";
}
return output;
}
function subtractMath() {
var input1 = document.getElementById("minput1").value;
var input2 = document.getElementById("minput2").value;
var input3 = document.getElementById("minput3").value;
var input4 = document.getElementById("minput4").value;
var input5 = document.getElementById("minput5").value;
if (input2 == "")
input2 = 0;
if (input3 == "")
input3 = 0;
if (input4 == "")
input4 = 0;
if (input5 == "")
input5 = 0;
var output = input1 - input2 - input3 - input4 - input5;
return output;
}
function clearMath()
{
document.getElementById("minput1").value = "";
document.getElementById("minput2").value = "";
document.getElementById("minput3").value = "";
document.getElementById("minput4").value = "";
document.getElementById("minput5").value = "";
document.getElementById("mcondbox").value = "";
}
function rtocMath()
{
var value1 = document.getElementById("minput1").value;
var output = Math.PI * value1 * value1;
return output;
}
function dtocMath()
{
var value1 = document.getElementById("minput1").value;
var output = Math.PI * value1;
return output;
}
function sqrtMath()
{
var value1 = document.getElementById("minput1").value;
var output = Math.sqrt(value1);
return output;
}
function pwrMath()
{
var value1 = document.getElementById("minput1").value;
var value2 = document.getElementById("minput2").value;
var output = Math.pow(value1, value2);
return output;
}
function dectodegMath()
{
var value1 = document.getElementById("minput1").value;
var output = value1 * 360;
return output;
}
function degtodecMath() {
var value1 = document.getElementById("minput1").value;
var output = value1 / 360;
return output;
}
function cosMath() {
var value1 = document.getElementById("minput1").value;
var output = Math.cos(value1);
return output;
}
function sinMath() {
var value1 = document.getElementById("minput1").value;
var output = Math.sin(value1);
return output;
}
function tanMath() {
var value1 = document.getElementById("minput1").value;
var output = Math.tan(value1);
return output;
}
function coinRand() {
var rand = Math.random();
if (rand <= 0.50)
var output = "Tails";
else
var output = "Heads";
return output;
}
function floatRand() {
return Math.random();
}
function percentRand() {
var rand = Math.random() * 100;
var output = Math.round(rand) + "%";
return output;
}
function intRand() {
var value1 = document.getElementById("minput1").value;
var value2 = document.getElementById("minput2").value;
var randroot = Math.random();
if (value2 == "max")
{
var length = value1.toString().length;
if (length > 16) {
alert("The number of digits is too high. Enter a number smaller than 17 digits long.");
return "Try again!!";
}
var mul = 1;
for (i = 0; i < length; i++)
var mul = mul * 10;
var result = randroot * mul
var parsed = parseInt(result)
while ((parsed > value1) || (parsed == 0)) {
randroot = Math.random();
result = randroot * mul;
parsed = parseInt(result);
}
var psl = parsed.toString().length;
while (psl < length) {
parsed = "0" + parsed;
psl = parsed.length;
}
return parsed;
}
else if ((value2 == "digits") || (value2 == "")) {
var length = value1.toString();
if (length > 16) {
alert("The number of digits is too high. Enter a number smaller than 17.");
return "Try again!!";
}
var mul = 1;
for (i = 0; i < length; i++)
var mul = mul * 10;
var result = randroot * mul;
var parsed = parseInt(result);
var psl = parsed.toString().length;
while (psl < length) {
parsed = "0" + parsed;
psl = parsed.length;
}
return parsed;
}
else {
alert("There was an invalid argument in the second box. Try either 'max' or 'digits'.");
return "Try again!!";
}
}
$(document).ready(function () {
var cond = document.getElementById("mcond");
$("#mcondsel").click(function () {
$("#mcondmenu").slideToggle("fast");
});
$("#addmathlist").click(function () {
cond.value = "add";
});
$("#minmathlist").click(function () {
cond.value = "subtract";
});
$("#mulmathlist").click(function () {
cond.value = "multiply";
});
$("#divmathlist").click(function () {
cond.value = "divide";
});
$("#sqrmathlist").click(function () {
cond.value = "square root";
});
$("#pwrmathlist").click(function () {
cond.value = "power";
});
$("#rtcmathlist").click(function () {
cond.value = "rtc";
});
$("#dtcmathlist").click(function () {
cond.value = "dtc";
});
$("#gtimathlist").click(function () {
cond.value = "degtodec";
});
$("#itgmathlist").click(function () {
cond.value = "dectodeg";
});
$("#cosmathlist").click(function () {
cond.value = "cosine";
});
$("#sinmathlist").click(function () {
cond.value = "sine";
});
$("#tanmathlist").click(function () {
cond.value = "tangent";
});
$("#flimathlist").click(function () {
cond.value = "coin flip";
doMath();
});
$("#dcrmathlist").click(function () {
cond.value = "floatrand";
doMath();
});
$("#prcmathlist").click(function () {
cond.value = "percent";
doMath();
});
$("#intmathlist").click(function () {
cond.value = "randint";
});
$("#minputtog").click(function () {
$("#minputmore").slideToggle("fast");
});
$("#arthtog").click(function () {
$("#arthlist").slideToggle("fast");
});
$("#spectog").click(function () {
$("#speclist").slideToggle("fast");
});
$("#trigtog").click(function () {
$("#triglist").slideToggle("fast");
});
$("#autotog").click(function () {
$("#autolist").slideToggle("fast");
});
$("#randtog").click(function () {
$("#randlist").slideToggle("fast");
});
$(".mathlist").click(function () {
$("#mcondmenu").slideToggle("fast");
$("#mcondsel").toggleClass("dtoggle utoggle");
clearMath();
});
});
$(function () {
$(document).tooltip();
});
// The half-finished array-based maths code. My favourite is findQuantArray().
function doArray() {
var arrCond = document.getElementById("acond").value;
if (arrCond = "mean")
var out = meanArray();
else if (arrCond = "median")
var out = medianArray();
else {
alert("The operator you have chosen is invalid. Please try again.");
var out = "Try again!!!";
}
var aout = document.getElementById("aout");
aout.value = out;
return out;
}
function getArray() {
var vArray = [];
var v0 = null;
var v1 = document.getElementById("a1").value;
var v2 = document.getElementById("a2").value;
var v3 = document.getElementById("a3").value;
var v4 = document.getElementById("a4").value;
var v5 = document.getElementById("a5").value;
var v6 = document.getElementById("a6").value;
var v7 = document.getElementById("a7").value;
var v8 = document.getElementById("a8").value;
var v9 = document.getElementById("a9").value;
var v10 = document.getElementById("a10").value;
var v11 = document.getElementById("a11").value;
var v12 = document.getElementById("a12").value;
var v13 = document.getElementById("a13").value;
var v14 = document.getElementById("a14").value;
var v15 = document.getElementById("a15").value;
var v16 = document.getElementById("a16").value;
var v17 = document.getElementById("a17").value;
var v18 = document.getElementById("a18").value;
var v19 = document.getElementById("a19").value;
var v20 = document.getElementById("a20").value;
vArray.push(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13,
v14, v15, v16, v17, v18, v19, v20);
return vArray;
}
function clearArray() {
document.getElementById("a1").value = "";
document.getElementById("a2").value = "";
document.getElementById("a3").value = "";
document.getElementById("a4").value = "";
document.getElementById("a5").value = "";
document.getElementById("a6").value = "";
document.getElementById("a7").value = "";
document.getElementById("a8").value = "";
document.getElementById("a9").value = "";
document.getElementById("a10").value = "";
document.getElementById("a11").value = "";
document.getElementById("a12").value = "";
document.getElementById("a13").value = "";
document.getElementById("a14").value = "";
document.getElementById("a15").value = "";
document.getElementById("a16").value = "";
document.getElementById("a17").value = "";
document.getElementById("a18").value = "";
document.getElementById("a19").value = "";
document.getElementById("a20").value = "";
}
function findQuantArray() {
var ar = getArray();
if (ar[20] !== "") // full input failsafe
return 20;
else {
var n = ar[0];
for (var i=1;n !== "";i++) { // checks to see first empty input
var n = ar[i];
}
i = i - 2; // makes number correct
return i;
}
}
function meanArray() {
var ar = getArray();
var quant = findQuantArray();
/* for (i=1;i < quant;i++) {
if (i == 1)
var result = addMath(ar[1],ar[2]);
else {
var o = i++;
result = addMath(result,ar[o]);
}
}*/
var result = 42;
var out = result / quant;
return out;
}
function addArray() {
var ar = getArray();
var qu = findQuantArray();
var rs = qu/5;
}
function medianArray() {
var quant = findQuantArray();
return parseInt(quant / 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment