Created
October 29, 2014 04:23
-
-
Save tpowell/4171a544d7ed87d2146f to your computer and use it in GitHub Desktop.
isOdd function from JS1 lecture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Odd Check!</title> | |
<style> | |
.errMsg {color: red; font-style: italic;} | |
output {background-color: orange; | |
width: 40%; text-align: center; | |
font-size: large; font-weight: bold; | |
display: block; | |
margin-top: 20px; | |
} | |
garbage { | |
color: green; | |
background: yellow; | |
display: block; | |
} | |
</style> | |
</head> | |
<body> | |
<script> | |
var JS1 = {}; // global wrapper object | |
JS1.DEBUG = false; | |
JS1.strings = []; | |
JS1.strings['checkError'] = "Integers only"; | |
JS1.strings['isOddError'] = "isOdd requires numbers"; | |
JS1.strings['isOddTrue'] = "Number is odd"; | |
JS1.strings['isOddFalse'] = "Number is even"; | |
/* | |
* log - show a trace statement appropriately based upon browser support | |
* | |
* params: msg - a string to output somehow | |
* returns: nothing | |
* | |
* Uses console.log or alert depending on support | |
*/ | |
JS1.log = function (msg) { | |
if (!JS1.DEBUG) { // if no debugging just return | |
return; | |
} | |
if ((console) && (console.log)) { | |
console.log(msg); | |
} | |
else if (alert) { | |
alert(msg) | |
} | |
else { | |
throw "Can't log error messages"; | |
} | |
} /* log */ | |
/* | |
* isOdd - returns true/false for if a passed number is odd or not | |
* | |
* params: val - an integer to check | |
* returns: true if an odd number, false if not | |
* | |
* Throws exception if sent wrong data type | |
*/ | |
JS1.isOdd = function (val) { | |
JS1.log("Entering function isOdd"); | |
if (typeof val != "number") { | |
throw JS1.strings['isOddError']; | |
} | |
JS1.log("isOdd calc = "+ ((val % 2) !== 0)); | |
return ((val % 2) !== 0); | |
} /* isOdd */ | |
/* | |
* checkNumber - reads a form field, checks to make sure | |
* values are integers, if not prints error message | |
* Calls isOdd to figure out result | |
* | |
* params: none | |
* returns: none | |
* | |
*/ | |
JS1.checkNumber = function () { | |
var el; | |
el = document.getElementById("numberToTest"); | |
var val = parseInt(el.value); | |
if (isNaN(val)) { | |
document.getElementById("errorMessage").innerHTML = JS1.strings['checkError']; | |
document.getElementById("result").innerHTML = ""; | |
} else { | |
document.getElementById("errorMessage").innerHTML = ""; | |
if (JS1.isOdd(val)) { | |
document.getElementById("result").innerHTML = JS1.strings['isOddTrue']; | |
} else { | |
document.getElementById("result").innerHTML = JS1.strings['isOddFalse']; | |
} | |
} | |
} | |
window.onload = function () { | |
document.getElementById("checkBtn").onclick = function () { | |
JS1.checkNumber(); | |
} | |
} | |
</script> | |
<h1>Odd Checker</h1> | |
<form action="#" method="get"> | |
<label for="numberToTest">Number:</label> | |
<input type="text" id="numberToTest" | |
title="Enter a number" size="4"> | |
<span id="errorMessage" class="errMsg"></span> | |
<br> | |
<input type="button" id="checkBtn" value="Check"> | |
</form> | |
<output id="result" for="numberToTest"> | |
</output> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment