From f9250938bc0c34c019f871427f7eb5083d121809 Mon Sep 17 00:00:00 2001 From: Kevin Gonzalez <19812642+typekev@users.noreply.github.com> Date: Wed, 12 Jun 2019 21:11:06 +0200 Subject: [PATCH 1/6] Merge hello-world reference into master (#2) * Hello world examples with documentation for es5 and es6 with import and export * Prettier comments * Link to detailed "strict mode" documentation * Move hello world references into hello-world dir in prep for merg * Added Hello World description in ReadMe, in hello-world dir --- README.md | 3 ++- hello-world/README.md | 3 +++ hello-world/index.js | 22 +++++++++++++++++++++ hello-world/varDeclaration.js | 36 +++++++++++++++++++++++++++++++++++ hello-world/varExport.js | 35 ++++++++++++++++++++++++++++++++++ hello-world/varImport.js | 28 +++++++++++++++++++++++++++ 6 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 hello-world/README.md create mode 100644 hello-world/index.js create mode 100644 hello-world/varDeclaration.js create mode 100644 hello-world/varExport.js create mode 100644 hello-world/varImport.js diff --git a/README.md b/README.md index 1b3a383..bcc622f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # javascript-references -Material to reference when working with JavaScript + +Material to reference when working with JavaScript diff --git a/hello-world/README.md b/hello-world/README.md new file mode 100644 index 0000000..962c72f --- /dev/null +++ b/hello-world/README.md @@ -0,0 +1,3 @@ +# Hello World in JavaScript + +A set of scripts that output the message "Hello World". Such a program is very simple in most programming languages, and is often used to illustrate the basic syntax of a programming language. See https://en.wikipedia.org/wiki/%22Hello,_World!%22_program diff --git a/hello-world/index.js b/hello-world/index.js new file mode 100644 index 0000000..f93ad8a --- /dev/null +++ b/hello-world/index.js @@ -0,0 +1,22 @@ +/** + * No need to "use strict" + * In this case even though we are not declaring this code as an ES6 module + * the following code contains no syntax that can be caught by strict mode. + * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode + * See ./varDeclaration.js for an example that can benefit from strict mode. + */ + +/** + * The Console object provides access to the browser's debugging console + * See https://developer.mozilla.org/en-US/docs/Web/API/console + * The Console object's methods include log() + * The Console method log() outputs a message to the console. + * The message is not limited to a single string. + * See https://developer.mozilla.org/en-US/docs/Web/API/Console/log + */ +console.log("Hello World"); + +/** + * The easiest way to test this file is to run the following line in your console: + * node index.js + */ diff --git a/hello-world/varDeclaration.js b/hello-world/varDeclaration.js new file mode 100644 index 0000000..ee30c26 --- /dev/null +++ b/hello-world/varDeclaration.js @@ -0,0 +1,36 @@ +/** + * "use strict" in this case + * We are not declaring this code as an ES6 module and the following code + * contains syntax that can be caught by strict mode. + * See ./index.js for an example that does not benefit from strict mode. + */ +"use strict"; + +/** + * The code below will declare a global string named helloWorld, + * this is valid ES5 code, in ES6 we can also use let or const. + * Normally in ES6 this would be declared as a const. + * See ./es6.js for an example using const. + */ +var helloWorld = "Hello World"; + +/** + * Strict mode will catch an undeclared variable i.e: + * helloWorld = "Hello World" + * The above commented line will throw an error in strict mode. + */ + +/** + * The Console object provides access to the browser's debugging console + * See https://developer.mozilla.org/en-US/docs/Web/API/console + * The Console object's methods include log() + * The Console method log() outputs a message to the console. + * The message is not limited to a single string. + * See https://developer.mozilla.org/en-US/docs/Web/API/Console/log + */ +console.log(helloWorld); + +/** + * The easiest way to test this file is to run the following line in your console: + * node varDeclaration.js + */ diff --git a/hello-world/varExport.js b/hello-world/varExport.js new file mode 100644 index 0000000..20de3d9 --- /dev/null +++ b/hello-world/varExport.js @@ -0,0 +1,35 @@ +/** + * Do not include "use strict" + * In this case we are declaring this code as an ES6 module, + * module code is always strict mode code. + * See http://www.ecma-international.org/ecma-262/6.0/#sec-strict-mode-code + * See ./varDeclaration.js for an example that is not in strict mode by default. + */ + +/** + * The code below will declare and export a string constant named helloWorld, + * this is valid ES6 code, in ES5 we can only use var. + * See ./varDeclaration.js for an example using var. + */ +export const helloWorld = "Hello World"; + +/** + * Strict mode will catch an undeclared variable i.e: + * helloWorld = "Hello World" + * The above commented line will throw an error in strict mode. + */ + +/** + * The Console object provides access to the browser's debugging console + * See https://developer.mozilla.org/en-US/docs/Web/API/console + * The Console object's methods include log() + * The Console method log() outputs a message to the console. + * The message is not limited to a single string. + * See https://developer.mozilla.org/en-US/docs/Web/API/Console/log + */ +console.log(helloWorld); + +/** + * The easiest way to test this file is to run the following line in your console + * nodemon varDeclaration.js + */ diff --git a/hello-world/varImport.js b/hello-world/varImport.js new file mode 100644 index 0000000..f91e1f1 --- /dev/null +++ b/hello-world/varImport.js @@ -0,0 +1,28 @@ +/** + * Do not include "use strict" + * In this case we are declaring this code as an ES6 module, + * module code is always strict mode code. + * See http://www.ecma-international.org/ecma-262/6.0/#sec-strict-mode-code + * See ./varDeclaration.js for an example that is not in strict mode by default. + */ + +/** + * The code below will declare and import a string constant named helloWorld. + * See ./varExport.js + */ +import { helloWorld } from "./varExport"; + +/** + * The Console object provides access to the browser's debugging console + * See https://developer.mozilla.org/en-US/docs/Web/API/console + * The Console object's methods include log() + * The Console method log() outputs a message to the console. + * The message is not limited to a single string. + * See https://developer.mozilla.org/en-US/docs/Web/API/Console/log + */ +console.log(helloWorld); + +/** + * The easiest way to test this file is to run the following line in your console: + * nodemon varDeclaration.js + */ From fde02fe44876faffa12e3edd8a5037c69ee010ab Mon Sep 17 00:00:00 2001 From: Kevin Gonzalez <19812642+typekev@users.noreply.github.com> Date: Mon, 1 Jul 2019 17:41:45 +0200 Subject: [PATCH 2/6] Fix typo, loss should be lose --- bubble-sort/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bubble-sort/index.js b/bubble-sort/index.js index c010ff7..7d28919 100644 --- a/bubble-sort/index.js +++ b/bubble-sort/index.js @@ -114,7 +114,7 @@ function bubbleSort(arr) { * For more information on variable declarations see * https://github.com/floatsoft/javascript-references/blob/hello-world/varDeclaration.js#L9-L15 * - * We do this so that we do not loss the value of arr[i] during the swapping process. + * We do this so that we do not lose the value of arr[i] during the swapping process. * This is because we are doing our element swapping in-place. * See https://en.wikipedia.org/wiki/In-place_algorithm */ From 57d36ac99f2573beb0beb65165d995d3694ed5aa Mon Sep 17 00:00:00 2001 From: Kevin Gonzalez <19812642+typekev@users.noreply.github.com> Date: Mon, 1 Jul 2019 17:43:45 +0200 Subject: [PATCH 3/6] Clarified the arr param reminder --- bubble-sort/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bubble-sort/index.js b/bubble-sort/index.js index 7d28919..0e1974c 100644 --- a/bubble-sort/index.js +++ b/bubble-sort/index.js @@ -98,7 +98,7 @@ function bubbleSort(arr) { * Our condition dictates that arr[i] must be greater than arr[i + 1] by employing the greater than operator. * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Greater_than_operator * - * As a reminder, arr is our single parameter being passed to our bubbleSort function. arr must be an Array. + * As a reminder, arr is the single parameter of our bubbleSort function and arr must be an Array. * See https://github.com/floatsoft/javascript-references/blob/bubble-sort/index.js#L13-L19 * * We can select elements from our array by referencing there index. From f943ed75d14fdf60653cf18e27d53be4af201621 Mon Sep 17 00:00:00 2001 From: Kevin Gonzalez <19812642+typekev@users.noreply.github.com> Date: Mon, 1 Jul 2019 17:52:26 +0200 Subject: [PATCH 4/6] Fix typo, temp should be tmp --- bubble-sort/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bubble-sort/index.js b/bubble-sort/index.js index 0e1974c..e826280 100644 --- a/bubble-sort/index.js +++ b/bubble-sort/index.js @@ -131,7 +131,7 @@ function bubbleSort(arr) { arr[i] = arr[i + 1]; /** - * We can now complete our swap by assigning arr[i + 1] to the value of temp which is equal to the previous value of arr[i]. + * We can now complete our swap by assigning arr[i + 1] to the value of tmp which is equal to the previous value of arr[i]. */ arr[i + 1] = tmp; From d37ebf9d34b9005c84e0db5b020fa2c7912b9bb8 Mon Sep 17 00:00:00 2001 From: Kevin Gonzalez <19812642+typekev@users.noreply.github.com> Date: Mon, 1 Jul 2019 17:59:12 +0200 Subject: [PATCH 5/6] Fix typo, reassignment should be reassigned --- bubble-sort/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bubble-sort/index.js b/bubble-sort/index.js index e826280..97e6695 100644 --- a/bubble-sort/index.js +++ b/bubble-sort/index.js @@ -139,7 +139,7 @@ function bubbleSort(arr) { * Our variable named swapped is now reassigned the boolean value of true. * This means that the condition of our do...while loop will be met. * Therefore our do...while loop will continue to execute until this block statement no longer executes, - * and subsequently our variable named swapped is never reassignment. + * and subsequently our variable named swapped is never reassigned. * In other words the condition of our if statement dictating that arr[i] must be greater than arr[i + 1] * must evaluate to a falsy value for each element pair in our array, arr, in order to exit from our do...while loop. */ From 840658cb7e9f22f41fefd74e787edd8de39d32af Mon Sep 17 00:00:00 2001 From: Kevin Gonzalez <19812642+typekev@users.noreply.github.com> Date: Mon, 1 Jul 2019 18:02:21 +0200 Subject: [PATCH 6/6] Fixed typo, is should be it --- bubble-sort/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bubble-sort/index.js b/bubble-sort/index.js index 97e6695..dcc1720 100644 --- a/bubble-sort/index.js +++ b/bubble-sort/index.js @@ -153,7 +153,7 @@ function bubbleSort(arr) { } } /** - * The condition on this do...while loop is that is must run at least once and until swapped is false. + * The condition of our do...while loop is that it must run at least once and until swapped is false. * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while#Syntax */ } while (swapped);