Created
July 6, 2020 10:58
-
-
Save ivanteoh/9a6b2299bfc2db37e973ef2557125baf to your computer and use it in GitHub Desktop.
Javascript: 101 Week 2 Track 1
This file contains 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
function absolute(number) { | |
if (number < 0) | |
return -number; | |
return number; | |
} |
This file contains 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
function greaterThan(number1) { | |
function test(number2) { | |
return number2 > number1; | |
} | |
return test; | |
} | |
var greaterThanTwenty = greaterThan(20); | |
alert(greaterThanTwenty(52)); | |
alert(greaterThanTwenty(10)); |
This file contains 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
//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) { | |
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]); | |
} | |
} |
This file contains 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 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) { | |
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]); | |
} | |
} | |
function draw(symbol) { | |
function plusDraw(sign) { | |
for (var j = 0; j < sign; j++) { | |
document.write("+"); | |
} | |
} | |
var total = 9; | |
var plus = (total - symbol)/2; | |
plusDraw(plus); | |
for (var j = 0; j < symbol; j++) { | |
document.write("@"); | |
} | |
plusDraw(plus); | |
document.write("<br />"); | |
} | |
iterateAndOperate([1, 3, 5, 3, 1], draw); | |
} | |
</script> | |
</head> | |
<body onload="load()"> | |
</body> | |
</html> |
This file contains 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
++++@++++ | |
+++@@@+++ | |
++@@@@@++ | |
+++@@@+++ | |
++++@++++ |
This file contains 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 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) { | |
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]); | |
} | |
} | |
function draw(symbol) { | |
for (var j = 0; j < symbol; j++) { | |
document.write("*"); | |
} | |
document.write("<br />"); | |
} | |
iterateAndOperate([1, 3, 5, 3, 1], draw); | |
} | |
</script> | |
</head> | |
<body onload="load()"> | |
</body> | |
</html> |
This file contains 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
* | |
*** | |
***** | |
*** | |
* |
This file contains 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 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> |
This file contains 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
switch (expression) { | |
case 'a': | |
case 'b': | |
case 'c': | |
alphabet(); | |
break; | |
default: | |
nonAlphabet(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment