CSC 107      Fall 2002

C. JavaScript Libraries

[ random.js | prompt.js | verify.js | char.js | array.js ]


// File: random.js // Author: Dave Reed // Date: 7/10/02 // // This file contains several routines for generating random values //////////////////////////////////////////////////////////////////////// function RandomNum(low, high) // Given : low <= high // Returns : a random number in the range [low, high) { return Math.random()*(high-low) + low; } function RandomInt(low, high) // Given : low <= high // Returns : a random integer in the range [low, high] { return Math.floor(Math.random()*(high-low+1)) + low; } function RandomChar(str) // Given : str is a nonempty string // Returns: a random character from the string { return str.charAt(RandomInt(0, str.length-1)); } function RandomOneOf(list) // Given : list is a nonempty list (array) // Returns: a random item from the list { return list[RandomInt(0, list.length-1)]; }


// File: verify.js // Author: Dave Reed // Date: 7/10/02 // // This library contains functions for verifying the contents of // a text box. For each function the name of a text box is passed // as input, along with any restrictions on the contents. If the // contents of the box are not legal, a warning is displayed. ///////////////////////////////////////////////////////////////////// function VerifyNum(textBox, resetValue) // Assumes: textBox is a text box in the page // Results: alert if textBox does not contain a number & clears contents { var boxValue; boxValue = parseFloat(textBox.value); if ( isNaN(boxValue) ) { alert("You must enter a number value!"); textBox.value = ""; } } function VerifyNumInRange(textBox, low, high, resetValue) // Assumes: textBox is a text box in the page // Results: alert if textBox does not contain an integer & clears contents { var boxValue; boxValue = parseFloat(textBox.value); if ( isNaN(boxValue) || (boxValue < low) || (boxValue > high)) { alert("You must enter a number value in the range [" + low + ".." + high + "]"); textBox.value = ""; } } function VerifyInt(textBox) // Assumes: textBox is a text box, low <= high, resetValue is optional // Results: alert if textBox does not contain a number in range & clears { var boxValue; boxValue = parseFloat(textBox.value); if ( isNaN(boxValue) || (boxValue % 1 != 0) ) { alert("You must enter an integer value!"); textBox.value = ""; } } function VerifyIntInRange(textBox, low, high) // Assumes: textBox is a text box, low <= high // Results: alert if textBox does not contain an integer in range & clears { var boxValue; boxValue = parseFloat(textBox.value); if ( isNaN(boxValue) || (boxValue % 1 != 0) || (boxValue < low) || (boxValue > high)) { alert("You must enter an integer value in the range [" + low + ".." + high + "]"); textBox.value = ""; } } function VerifyOneOf(textBox, options) // Assumes: textBox is a text box, options is a list, resetValue is optional // Results: alert if textBox does not contain one of options & clears { var i; for (i = 0; i < options.length; i++) { if (textBox.value == options[i]) { return; } } alert("You must enter one of the following: " + options); textBox.value = ""; }


// File: prompt.js // Author: Dave Reed // Date: 7/10/02 // // This file contains several routines for prompting and reading values //////////////////////////////////////////////////////////////////////// function PromptNum(message) // Given : message is a string // Returns: number value read in from user (using prompt message) { var value, parsed; value = prompt(message, ""); while (true) { if (value == null) { return null; } parsed = parseFloat(value); if (!isNaN(parsed)) { return parsed; } value = prompt(message, "A NUMBER"); } } function PromptNumInRange(message, low, high) // Given : message is a string, low < high // Returns: value read in from user in range [low, high) { var value, parsed; value = prompt(message, ""); while (true) { if (value == null) { return null; } parsed = parseFloat(value); if (!isNaN(parsed) && low <= parsed && parsed < high) { return parsed; } value = prompt(message, low + " <= NUMBER < " + high); } } function PromptInt(message) // Given : message is a string // Returns: integer value read in from user (using prompt message) { var value, parsed; value = prompt(message, ""); while (true) { if (value == null) { return null; } parsed = parseFloat(value); if (!isNaN(parsed) && (parsed % 1 == 0)) { return parsed; } value = prompt(message, "AN INTEGER"); } } function PromptIntInRange(message, low, high) // Given : message is a string, low <= high (both integers) // Returns: integer value read in from user in range [low,high] { var value, parsed; value = prompt(message, ""); while (true) { if (value == null) { return null; } parsed = parseFloat(value); if (!isNaN(parsed) && (parsed % 1 == 0) && low <= parsed && parsed <= high) { return parsed; } value = prompt(message, low + " <= INTEGER <= " + high); } } function PromptOneOf(message, options) // Given : message is a string, options is a nonempty array // Returns: item read in from user contained in the options array { var value, i; value = prompt(message, ""); while (true) { if (value == null) { return null; } for (i = 0; i < options.length; i++) { if ((typeof(options[i]) == "number" && parseFloat(value) == options[i]) || (value == options[i])) { return options[i]; } } value = prompt(message, options.join(" OR ")); } }


// File: char.js // Author: Dave Reed // Date: 7/10/02 // // This file contains several routines for recognizing characters. //////////////////////////////////////////////////////////////////////// function IsLower(ch) // Given : ch is a character // Returns : true if ch is a lower-case letter { var lowers; lowers = "abcdefghijklmnopqrstuvwxyz"; return (lowers.indexOf(ch) != -1); } function IsUpper(ch) // Given : ch is a character // Returns : true if ch is a upper-case letter { var uppers; uppers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; return (uppers.indexOf(ch) != -1); } function IsLetter(ch) // Given : ch is a character // Returns : true if ch is a letter { return (IsUpper(ch) || IsLower(ch)); } function IsVowel(ch) // Given : ch is a character // Returns : true if ch is a vowel { var vowels; vowels = "aeiouAEIOU"; return (vowels.indexOf(ch) != -1); } function IsConsonant(ch) // Given : ch is a character // Returns : true if ch is a consonant { return (IsLetter(ch) && !IsVowel(ch)); } function IsDigit(ch) // Given : ch is a character // Returns : true if ch is a digit { var digits; digits = "0123456789"; return (digits.indexOf(ch.toLowerCase()) != -1); } function IsWhitespace(ch) // Given : ch is a character // Returns : true if ch is whitespace { var white; white = " \t\n\r"; return (white.indexOf(ch.toLowerCase()) != -1); }


// File: array.js // Author: Dave Reed // Date: 7/10/02 // // This file contains a routine for converting a string to an array. //////////////////////////////////////////////////////////////////////// function IsWhite(ch) // Given : ch is a character // Returns : true if ch is a whitecase letter { var white; white = " \t\n\r"; return (white.indexOf(ch) != -1); } function StringToArray(str) // Assumes: str is a sequence of words, separated by whitespace // Returns: an array containing the individual words { var items, count, index; items = []; count = 0; while (str != "") { index = 0; while (index < str.length && IsWhite(str.charAt(index))) { index++; } if (index < str.length) { var item = ""; while (index < str.length && !IsWhite(str.charAt(index))) { item += str.charAt(index); index++; } items[count] = item count++; } str = str.substring(index+1, str.length); } return items; } function ParseArray(strArray) // Assumes: strArray is an array of strings repr. numbers // Returns: a copy of array with elements converted to numbers { var index; index = 0; while (index < strArray.length) { strArray[index] = parseFloat(strArray[index]); index = index + 1; } return strArray; }