Skip to content

Instantly share code, notes, and snippets.

@codewizard13
Created May 9, 2015 20:36
Show Gist options
  • Select an option

  • Save codewizard13/d3fc731f2fd15aaa2250 to your computer and use it in GitHub Desktop.

Select an option

Save codewizard13/d3fc731f2fd15aaa2250 to your computer and use it in GitHub Desktop.
JavaScript code to Demonstrate my understanding of the "Math" object. I also write a simple FIBONACCI number generator.
<!DOCTYPE html>
<html>
<head>
<title>ERIC'S CODE - Not A Number</title>
<style type="text/css">
.ans {
color: green;
font-weight: bold;
background-color: yellow;
border: sold black 1px;
}
</style>
</head>
<body>
<h1>Demonstration Program:<br />
Demonstrates several functions in the JavaScript "Math" object
</h1>
<hr />
<script type="text/javascript">
// CONSTANTS:
var tab3 = "&nbsp;&nbsp;&nbsp;";
// writes val to browser and carriage returns
function print(val) {
document.write(val + "<br /><hr />");
}
// Math.round
function round(val) {
return Math.round(val);
}
// Math.max
function maxOf3(a,b,c) {
var max = Math.max(a,b,c);
return max;
}
// Math.min
function minOf3(a,b,c) {
var min = Math.min(a,b,c);
return min;
}
// Math.PI
function circleArea(num) {
// PI * r * r
var area = Math.PI * num * num;
return area;
}
function circumference(num) {
// 2 * PI * r
var circ = Math.PI * num * 2;
return circ;
}
// Math.random
// Math.sqrt
// Math.log
// generates an array of fibonacci number to
// "iterations" number of iterations. If no number
// given, will do 5 iterations.
function fibonacci(iterations) {
// create and initialize new array to hold our results
var fibArray = new Array();
var prevNum = 0;
var thisNum = 0;
var nextNum = 0;
// console.log(prevNum)
for (i=0; i<iterations; ++i) {
fibArray.push(prevNum);
// console.log("\tprevNum = " + prevNum);
if (prevNum == 0) {
thisNum = 1;
}
// console.log("iteration # " + (i+1) + ": ");
// console.log("\tthisNum = " + thisNum);
nextNum = prevNum + thisNum;
// console.log("\tnextNum = " + nextNum);
prevNum = thisNum;
thisNum = nextNum;
}
console.info("fibArray(" + iterations + ") = " + fibArray);
return fibArray;
}
fibonacci(0);
fibonacci(3);
fibonacci(5);
fibonacci(10);
fibonacci(21);
// define test radius
var radius = 3;
// define long decimal floating point number
var fpnum = 1.458398594859;
// define three integers as values for comparisons
var num1 = 34;
var num2 = 14;
var num3 = 25;
// define 3 floating point numbers for comparison
var fp1 = 3443.993924546545;
var fp2 = 2.656563565565;
var fp3 = 2.6565651205051;
/*
WRITE TO PAGE
*/
var out = ''; // var to accumulate output
// Round
out += "<b>TESTING: round()</b><br />";
out += "<span>" + fpnum + " rounded = <span class='ans'>" + round(fpnum) + "</span></span><hr />";
// Max of 3
out += "<b>TESTING: maxOf3()</b><br />"
out += "<span>Of these 3 values [" + num1 + "," + num2 + "," + num3 +
"], <span class='ans'>" + maxOf3(num1,num2,num3) + "</span> is GREATEST</span><hr />";
// Min of 3 INTEGERS
out += "<b>TESTING: minOf3() with INTEGERS</b><br />"
out += "<span>Of these 3 values [" + num1 + "," + num2 + "," + num3 +
"], <span class='ans'>" + minOf3(num1,num2,num3) + "</span> is SMALLEST</span><hr />";
// Min of 3 FLOATING POINT numbers
out += "<b>TESTING: minOf3() with FLOATING POINT numbers</b><br />"
out += "<span>Of these 3 values [" + fp1 + ", " + fp2 +
", " + fp3 + "], <span class='ans'>" +
minOf3(fp1,fp2,fp3) +
"</span> is SMALLEST</span><hr />";
// Circle Area
out += "<b>TESTING: circleArea()</b><br />"
out += "<span>If RADIUS = " + radius +
", then area = <span class='ans'>" + circleArea(radius) +
"</span></span><hr />";
// Circle Circumference
out += "<b>TESTING: circumference()</b><br />"
out += "<span>If RADIUS = " + radius +
", then circumference = <span class='ans'>" + circumference(radius) +
"</span></span><hr />";
// write everything to browser
document.write(out);
/*
NOTES:
* 2015.05.09 - ALL WORKS!
* Fibonacci works!!!! :)
*/
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment