Skip to main content

Ivan Teoh

Something personal yet public

166-homework33.html

(Source)

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <title>JavaScript 101</title>
    <script type="text/javascript">
        function load()
        {
            // A constant to hold the String "null". To be used in typeof checks
            NULL_VAL = "null";
            // A constant to hold the String "undefined". To be used in typeof
            // checks
            UNDEFINED_VAL = "undefined";

            /*
             * This function checks if the specified parameter is null or
             * undefined
             * @param something The specified parameter to check for a null or
             * undefined value
             * @param name The name of the parameter. This will be used in the
             * error message
             * If the value 'something' is found to be null or undefined, then
             * this method
             * will throw an Error
             */
            function checkNullOrUndefined(something, name) {
                if(UNDEFINED_VAL == typeof(something)) {
                    throw new Error(name + " cannot be undefined");
                }
                if(NULL_VAL == typeof(something)) {
                    throw new Error(name + " cannot be null");
                }
            }


            /*
             * This function accepts an array object and a function reference.
             * It iterates through the array and invokes the specified function
             * for every element of the array. In each invocation the current
             * array element is given to the function as a parameter.
             * @param arr The array
             * @param func The function to invoke for every element of the array
             * This method does not return any specific value.
             * This method throws an Error if 'arr' is null, undefined, or is
             * not an array
             * This method throws an Error if 'func' is null, undefined, or is
             * not a function
             */
            function iterateAndOperate(arr, func) {
                try {
                    checkNullOrUndefined(arr, "arr");
                    checkNullOrUndefined(func, "func");
                    // Verify that arr is an array
                    if(!(arr instanceof Array)) {
                        throw new Error("arr does not seem to be an array");
                    }
                    // Verify that arr is an array
                    if("function" != typeof(func)) {
                        throw new Error("func is not a function");
                    }
                    for(var i = 0; i < arr.length; i++) {
                        func(arr[i]);
                    }
                }
                catch(e){
                 alert('An error has occurred: '+e.message)
                }
            }
            iterateAndOperate();
        }
    </script>
</head>
<body onload="load()">
</body>
</html>