diff --git a/arrays/README.md b/arrays/README.md index 0690816e..e4448fc4 100644 --- a/arrays/README.md +++ b/arrays/README.md @@ -1,6 +1,6 @@ # Arrays -Arrays are a fundamental part of programming. An array is a list of data. We can store a lot of data in one variable, which makes our code more readable and easier to understand. It also makes it much easier to perform functions on related data. +Arrays are a fundamental part of programming. An array is a list of data. We can store large amounts of data in one variable, which makes our code more readable and easier to understand. It also makes it much easier to perform functions on related data. The data in arrays are called **elements**. diff --git a/arrays/indices.md b/arrays/indices.md index cddbbb7f..0a442956 100644 --- a/arrays/indices.md +++ b/arrays/indices.md @@ -1,6 +1,6 @@ # Indices -So you have your array of data elements, but what if you want to access a specific element? That is where indices come in. An **index** refers to a spot in the array. indices logically progress one by one, but it should be noted that the first index in an array is 0, as it is in most languages. Brackets [] are used to signify you are referring to an index of an array. +So you have your array of data elements, but what if you want to access a specific element? That is where indices come in. An **index** refers to a spot in the array. indices logically progress one by one, but it should be noted that the first index in an array is 0, as it is in most languages. Brackets, "[" and "]", are used to signify you are referring to an index of an array. ```javascript // This is an array of strings diff --git a/arrays/length.md b/arrays/length.md index 78deb9f4..78c1057e 100644 --- a/arrays/length.md +++ b/arrays/length.md @@ -1,6 +1,6 @@ # Length -Arrays have a property called length, and it's pretty much exactly as it sounds, it's the length of the array. +Arrays have a property called length, and it's pretty much exactly as it sounds. The length property is the length of the array. ```javascript var array = [1 , 2, 3]; diff --git a/conditional/README.md b/conditional/README.md index e0dc77ca..1383ec66 100644 --- a/conditional/README.md +++ b/conditional/README.md @@ -2,8 +2,8 @@ A condition is a test for something. Conditions are very important for programming, in several ways: -First of all conditions can be used to ensure that your program works, regardless of what data you throw at it for processing. If you blindly trust data, you’ll get into trouble and your programs will fail. If you test that the thing you want to do is possible and has all the required information in the right format, that won’t happen, and your program will be a lot more stable. Taking such precautions is also known as programming defensively. +First of all, conditions can be used to ensure that your program works, regardless of what data you throw at it for processing. If you blindly trust data, you’ll get into trouble and your programs will fail. If you test that the thing you want to do is possible and has all the required information in the right format, that won’t happen, and your program will be much more stable. Taking such precautions is also known as programming defensively. The other thing conditions can do for you is allow for branching. You might have encountered branching diagrams before, for example when filling out a form. Basically, this refers to executing different “branches” (parts) of code, depending on if the condition is met or not. -In this chapter, we'll learn the base of conditional logic in Javascript. \ No newline at end of file +In this chapter, we'll learn the basics of conditional logic in Javascript. \ No newline at end of file diff --git a/conditional/comparators.md b/conditional/comparators.md index 3cc21648..f51cdcc2 100644 --- a/conditional/comparators.md +++ b/conditional/comparators.md @@ -8,16 +8,16 @@ if (country === "France") { } ``` -The conditional part is the variable `country` followed by the three equal signs (`===`). Three equal signs tests if the variable `country` has both the correct value (`France`) and also the correct type (`String`). You can test conditions with double equal signs, too, however a conditional such as `if (x == 5)` would then return true for both `var x = 5;` and `var x = "5";`. Depending on what your program is doing, this could make quite a difference. It is highly recommended as a best practice that you always compare equality with three equal signs (`===` and `!==`) instead of two (`==` and `!=`). +The conditional part is the variable `country` followed by the three equal signs (`===`). Three equal signs tests if the variable `country` has both the correct value (`France`) and also the correct type (`String`). You can test conditions with double equal signs, too, however a conditional such as `if (x == 5)` would then return true for both `var x = 5;` and ``var x = '5';``. Depending on what your program is doing, this could make quite a difference. It is highly recommended as a best practice that you always compare equality with three equal signs (`===` and `!==`) instead of two (`==` and `!=`). Other conditional test: -* ```x > a```: is x bigger than a? -* ```x < a```: is x less than a? -* ```x <= a```: is x less than or equal to a? -* ```x >=a```: is x greater than or equal to a? -* ```x != a```: is x not a? -* ```x```: does x exist? +* `x > a`: is x bigger than a? +* `x < a`: is x less than a? +* `x <= a`: is x less than or equal to a? +* `x >=a`: is x greater than or equal to a? +* `x != a`: is x not a? +* `x`: does x exist? {% exercise %} diff --git a/conditional/concatenate.md b/conditional/concatenate.md index d31b0565..92e4cf9d 100644 --- a/conditional/concatenate.md +++ b/conditional/concatenate.md @@ -1,8 +1,8 @@ # Concatenate conditions -Furthermore you can concatenate different conditions with "or” or “and” statements, to test whether either statement is true, or both are true, respectively. +Furthermore, you can concatenate different conditions with "or” or “and” statements to test whether either statement is true, or both are true, respectively. -In JavaScript “or” is written as `||` and “and” is written as `&&`. +In JavaScript, “or” is written as `||` and “and” is written as ``&&``. Say you want to test if the value of x is between 10 and 20—you could do that with a condition stating: @@ -20,10 +20,9 @@ if(country === 'England' || country === 'Germany') { } ``` -**Note**: Just like operations on numbers, Condtions can be grouped using parenthesis, ex: ```if ( (name === "John" || name === "Jennifer") && country === "France")```. {% exercise %} -Fill up the 2 conditions so that `primaryCategory` equals `"E/J"` only if name equals `"John"` and country is `"England"`, and so that `secondaryCategory` equals `"E|J"` only if name equals `"John"` or country is `"England"` +Fill up the 2 conditions so that primaryCategory equals "E/J" only if name equals "John" and country is "England", and so that secondaryCategory equals "E|J" only if name equals "John" or country is "England". {% initial %} var name = "John"; var country = "England"; diff --git a/functions/README.md b/functions/README.md index 76f2091d..de45a848 100644 --- a/functions/README.md +++ b/functions/README.md @@ -1,6 +1,6 @@ # Functions -Functions, are one of the most powerful and essential notions in programming. +Functions are one of the most powerful and essential notions in programming. -Functions like mathematical functions perform transformations, they take input values called **arguments** and **return** an output value. +Functions, like mathematical functions perform transformations, take input values called **arguments** and **return** an output value. diff --git a/functions/declare.md b/functions/declare.md index cf4e8b31..ffd24b68 100644 --- a/functions/declare.md +++ b/functions/declare.md @@ -1,6 +1,6 @@ # Declaring Functions -Functions, like variables, must be declared. Let's declare a function `double` that accepts an **argument** called `x` and **returns** the double of x : +Functions, like variables, must be declared. Let's declare a function `double` that accepts an **argument** called `x` and **returns** the double of x: ```javascript function double(x) { @@ -8,9 +8,9 @@ function double(x) { } ``` ->*Note:* the function above **may** be referenced before it has been defined. +**Note:** the function above **may** be referenced before it has been defined. -Functions are also values in JavaScript; they can be stored in variables (just like numbers, strings, etc ...) and given to other functions as arguments : +Functions are also values in JavaScript. They can be stored in variables (just like numbers, strings, etc...) and given to other functions as arguments: ```javascript var double = function(x) { @@ -18,7 +18,7 @@ var double = function(x) { }; ``` ->*Note:* the function above **may not** be referenced before it is defined, just like any other variable. +**Note:** the function above **may not** be referenced before it is defined, just like any other variable. {% exercise %} Declare a function named `triple` that takes an argument and returns its triple. diff --git a/functions/higher_order.md b/functions/higher_order.md index 66984ad9..bb504db6 100644 --- a/functions/higher_order.md +++ b/functions/higher_order.md @@ -1,23 +1,22 @@ # Higher Order Functions -Higher order functions are functions that manipulate other functions. -For example, a function can take other functions as arguments and/or produce a function as its return value. -Such *fancy* functional techniques are powerful constructs available to you in JavaScript and other high-level languages like python, lisp, etc. +Higher order functions are functions that manipulate other functions. For example, a function can take other functions as arguments and/or produce a function as its return value. Such *fancy* functional techniques are powerful constructs available to you in JavaScriptm as well as in other high-level languages like Python and Lisp. -We will now create two simple functions, `add_2` and `double`, and a higher order function called `map`. `map` will accept two arguments, `func` and `list` (its declaration will therefore begin `map(func,list)`), and return an array. `func` (the first argument) will be a function that will be applied to each of the elements in the array `list` (the second argument). +We will now create two simple functions, `add_2` and `double`, and a higher order function called `map`. The function `map` will accept two arguments, `func` and `list` (its declaration will therefore begin `map(func,list)`), and return an array. The function `func` (the first argument) will be applied to each of the elements in the array `list` (the second argument). ```javascript // Define two simple functions var add_2 = function(x) { return x + 2; }; + var double = function(x) { return 2 * x; }; -// map is cool function that accepts 2 arguments: -// func the function to call -// list a array of values to call func on +// map is a cool function that accepts 2 arguments: +// func the function to call +// list a array of values to call func on var map = function(func, list) { var output=[]; // output list for(idx in list) { @@ -26,7 +25,6 @@ var map = function(func, list) { return output; } - // We use map to apply a function to an entire list // of inputs to "map" them to a list of corresponding outputs map(add_2, [5,6,7]) // => [7, 8, 9] @@ -48,8 +46,7 @@ process_add_2([5,6,7]) // => [7, 8, 9] process_double([5,6,7]) // => [10, 12, 14] ``` -Now let's create a function called `buildProcessor` that takes a function `func` as input -and returns a `func`-processor, that is, a function that applies `func` to each input in list. +Now let's create a function called `buildProcessor` that takes a function `func` as input and returns a `func` processor, that is, a function that applies `func` to each input in list. ```javascript // a function that generates a list processor that performs @@ -72,7 +69,7 @@ process_double([5,6,7]) // => [10, 12, 14] Let's look at another example. -We'll create a function called `buildMultiplier` that takes a number `x` as input and returns a function that multiplies its argument by `x` : +We'll create a function called `buildMultiplier` that takes a number (`x`) as input and returns a function that multiplies its argument by that number: ```javascript var buildMultiplier = function(x) { @@ -91,7 +88,7 @@ triple(3); // => 9 {% exercise %} Define a function named `negate` that takes `add1` as argument and returns a function, that returns the negation of the value returned by `add1`. (Things get a bit more complicated ;) ) {% initial %} -var add1 = function (x) { +var add1 = function(x) { return x + 1; }; @@ -104,7 +101,7 @@ var negate = function(func) { negate(add1)(5); {% solution %} -var add1 = function (x) { +var add1 = function(x) { return x + 1; } diff --git a/loops/README.md b/loops/README.md index d41b905f..e8a17c4e 100644 --- a/loops/README.md +++ b/loops/README.md @@ -1,6 +1,6 @@ # Loops -Loops are repetitive conditions where one variable in the loop changes. Loops are handy, if you want to run the same code over and over again, each time with a different value. +Loops are repetitive conditions where one variable in the loop changes. Loops are handy if you want to run the same code over and over again, each time with a different value. Instead of writing: diff --git a/loops/dowhile.md b/loops/dowhile.md index ad3e9fe7..1523e125 100644 --- a/loops/dowhile.md +++ b/loops/dowhile.md @@ -1,7 +1,7 @@ # Do...While Loop The do...while statement creates a loop that executes a specified statement until the test condition evaluates to be false. The condition is evaluated after executing the statement. -Syntax for do... while is +Syntax for do...while is ```javascript do{ @@ -10,7 +10,7 @@ do{ while(expression) ; ``` -Lets for example see how to print numbers less than 10 using `do...while` loop: +Let's look at the following example to see how to print numbers less than 10 using a `do...while` loop: ``` var i = 0; @@ -20,7 +20,7 @@ do { } while (i < 10); ``` ->***Note***: `i = i + 1` can be written `i++`. +***Note***: `i = i + 1` can be written `i++`. {% exercise %} diff --git a/loops/for.md b/loops/for.md index 4dbd89e7..a12105b7 100644 --- a/loops/for.md +++ b/loops/for.md @@ -1,6 +1,6 @@ # For Loop -The easiest form of a loop is the for statement. This one has a syntax that is similar to an if statement, but with more options: +The easiest form of a loop is the `for` statement. The `for` statement has a syntax that is similar to an `if` statement, but with more options: ```javascript for(condition; end condition; change){ @@ -8,7 +8,7 @@ for(condition; end condition; change){ } ``` -Lets for example see how to execute the same code ten-times using a `for` loop: +Let's look at the following example see how to execute the same code ten-times using a `for` loop: ``` for(var i = 0; i < 10; i = i + 1){ @@ -16,11 +16,11 @@ for(var i = 0; i < 10; i = i + 1){ } ``` ->***Note***: `i = i + 1` can be written `i++`. +***Note***: `i = i + 1` can be written `i++`. {% exercise %} -Using a for-loop, create a variable named `message` that equals the concatenation of integers (0, 1, 2, ...) from 0 to 99. +Using a for loop, create a variable named message that equals the concatenation of integers (0, 1, 2, ...) from 0 to 99. {% initial %} var message = ""; {% solution %} diff --git a/loops/while.md b/loops/while.md index bcc3ffb0..572d36ea 100644 --- a/loops/while.md +++ b/loops/while.md @@ -1,6 +1,6 @@ # While Loop -While Loops repetitively execute a block of code as long as a specified condition is true. +While loops repetitively execute a block of code as long as a specified condition is true. ```javascript while(condition){ @@ -8,7 +8,7 @@ while(condition){ } ``` -For example, the loop in this example will repetitively execute its block of code as long as the variable i is less than 5: +For example, the loop in the code below will repetitively execute as long as the variable i is less than 5: ```javascript var i = 0, x = ""; @@ -18,7 +18,7 @@ while (i < 5) { } ``` -The Do/While Loop is a variant of the while loop. This loop will execute the code block once before checking if the condition is true. It then repeats the loop as long as the condition is true: +The do...while loop is a variant of the while loop. This loop will execute the code block once before checking if the condition is true. It then repeats the loop as long as the condition is true: ```javascript do { @@ -31,7 +31,7 @@ do { {% exercise %} -Using a while-loop, create a variable named `message` that equals the concatenation of integers (0, 1, 2, ...) as long as its length (`message.length`) is less than 100. +Using a while-loop, create a variable named message that equals the concatenation of integers (0, 1, 2, ...) as long as its length (message.length) is less than 100. {% initial %} var message = ""; {% solution %} diff --git a/objects/README.md b/objects/README.md index 12730441..393496d5 100644 --- a/objects/README.md +++ b/objects/README.md @@ -1,4 +1,4 @@ # Objects -The primitive types of JavaScript are `true`, `false`, numbers, strings, `null` and `undefined`. **Every other value is an `object`.** +The primitive types of JavaScript are `true`, `false`, numbers, strings, `null`, and `undefined`. **Every other value is an `object`.** -In JavaScript objects contain `propertyName`: `propertyValue` pairs. +In JavaScript, objects contain `propertyName`: `propertyValue` pairs. diff --git a/objects/creation.md b/objects/creation.md index e1a82b3a..5c0ff8a3 100644 --- a/objects/creation.md +++ b/objects/creation.md @@ -1,19 +1,19 @@ # Creation -There are two ways to create an `object` in JavaScript: +There are two ways to create an `object` in JavaScript. -1. literal +1. Literal: ```js var object = {}; // Yes, simply a pair of curly braces! ``` - > ***Note:*** this is the **recomended** way. +***Note:*** this is the **recomended** way. -2. and object-oriented +2. Object-oriented: ```js var object = new Object(); ``` - > ***Note:*** it's almost like Java. +***Note:*** it's almost like Java. diff --git a/objects/delete.md b/objects/delete.md index b4b5cf1b..d0b34fc8 100644 --- a/objects/delete.md +++ b/objects/delete.md @@ -1,14 +1,14 @@ # Delete -`delete` can be used to **remove a property** from an object. It will remove a property from the -object if it has one. It will not look further in the prototype chain. -Removing a property from an object may allow a property from the prototype chain to shine through: +`delete` can be used to **remove a property** from an object, but it will not look further in the prototype chain. Removing a property from an object may allow a property from the prototype chain to shine through: ```js var adult = {age:26}, child = Object.create(adult); child.age = 8; delete child.age; - /* Remove age property from child, revealing the age of the prototype, because then it is not overriden. */ + /* Remove age property from child, revealing the age of the prototype, + because then it is not overriden. */ + var prototypeAge = child.age; // 26, because child does not have its own age property. ``` diff --git a/objects/global_footprint.md b/objects/global_footprint.md index 86de8075..5beb0909 100644 --- a/objects/global_footprint.md +++ b/objects/global_footprint.md @@ -1,5 +1,5 @@ # Global footprint -If you are developing a module, which might be running on a web page, which also runs other modules, then you must beware the variable name overlapping. +If you are developing a module runs on a web page and also runs other modules, then you must be aware the variable name overlapping. Suppose we are developing a counter module: ```js @@ -13,6 +13,6 @@ var myCounter = { } } ``` -> ***Note:*** this technique is often used with closures, to make the internal state immutable from the outside. +**Note:** this technique is often used with closures, to make the internal state immutable from the outside. -The module now takes only one variable name — `myCounter`. If any other module on the page makes use of such names like `number` or `isGreaterThanTen` then it's perfectly safe, because we will not override each others values; +The module now takes only one variable name — `myCounter`. If any other module on the page uses variables like `number` or `isGreaterThanTen`, then it's perfectly safe. Because those variables are within the closures of `myCounter`, we will not override those values. diff --git a/objects/properties.md b/objects/properties.md index 1581c5b4..30a8fba0 100644 --- a/objects/properties.md +++ b/objects/properties.md @@ -1,5 +1,6 @@ # Properties -Object's property is a `propertyName`: `propertyValue` pair, where **property name can be only a string**. If it's not a string, it gets casted into a string. You can specify properties **when creating** an object **or later**. There may be zero or more properties separated by commas. +Object's property is a `propertyName`: `propertyValue` pair, where the **property name can be only a string**. If it's not a string, it gets casted into a string. You can specify properties **when creating** an object **or later**. There may be zero or more properties separated by commas. + ```js var language = { name: 'JavaScript', @@ -17,16 +18,22 @@ var language = { }; ``` -The following code demonstates how to **get** a property's value. +The following code demonstates how to **get** a property's value: + ```js var variable = language.name; // variable now contains "JavaScript" string. + variable = language['name']; - // The lines above do the same thing. The difference is that the second one lets you use litteraly any string as a property name, but it's less readable. + // The lines above do the same thing. The difference is that the second one + // lets you use literally any string as a property name, but it's less + // readable. + variable = language.newProperty; // variable is now undefined, because we have not assigned this property yet. ``` -The following example shows how to **add** a new property **or change** an existing one. +The following example shows how to **add** a new property **or change** an existing one: + ```js language.newProperty = 'new value'; // Now the object has a new property. If the property already exists, its value will be replaced. diff --git a/objects/prototype.md b/objects/prototype.md index 43bb815a..c580bc84 100644 --- a/objects/prototype.md +++ b/objects/prototype.md @@ -1,43 +1,50 @@ # Prototype Every object is linked to a prototype object from which it inherits properties. -All objects created from object literals (`{}`) are automatically linked to Object.prototype, which is an object that comes standard with JavaScript. +All objects that are created from object literals (`{}`) are automatically linked to Object.prototype, which is an object that comes standard with JavaScript. -When a JavaScript interpreter (a module in your browser) tries to find a property, which You want to retrieve, like in the following code: +The following code can be used to retrieve an object's property: ```js var adult = {age: 26}, retrievedProperty = adult.age; // The line above ``` -First, the interpreter looks through every property the object itself has. For example, `adult` has only one own property — `age`. But besides that one it actually has a few more properties, which were inherited from Object.prototype. +First, the interpreter looks through every property the object itself has. For example, `adult` has only one own property — `age`. However, it actually has a few more properties, which are inherited from Object.prototype. ```js var stringRepresentation = adult.toString(); // the variable has value of '[object Object]' ``` -`toString` is an Object.prototype's property, which was inherited. It has a value of a function, which returns a string representation of the object. If you want it to return a more meaningful representation, then you can override it. Simply add a new property to the adult object. +The property `toString` is an Object.prototype's property, which was inherited. It has a value of a function, which returns a string representation of the object. If you want it to return a more meaningful representation, then you can override it by simply adding a new property to the adult object. ```js adult.toString = function(){ return "I'm "+this.age; } ``` -If you call the `toString` function now, the interpreter will find the new property in the object itself and stop. +If you call the `toString` function now, the interpreter will find the new property in the object itself and stop. The interpreter retrieves the first property it will find from the object itself, then continue through its prototype. -Thus the interpreter retrieves the first property it will find on the way from the object itself and further through its prototype. - -To set your own object as a prototype instead of the default Object.prototype, you can invoke `Object.create` as follows: +To set your own object as a prototype, instead of the default Object.prototype, you can invoke `Object.create` as follows: ```js var child = Object.create(adult); - /* This way of creating objects lets us easily replace the default Object.prototype with the one we want. In this case, the child's prototype is the adult object. */ + /* This way of creating objects lets us easily replace the default + Object.prototype with the one we want. In this case, the child's prototype is + the adult object. */ + child.age = 8; /* Previously, child didn't have its own age property, and the interpreter had to look further to the child's prototype to find it. Now, when we set the child's own age, the interpreter will not go further. Note: adult's age is still 26. */ + var stringRepresentation = child.toString(); // The value is "I'm 8". - /* Note: we have not overridden the child's toString property, thus the adult's method will be invoked. If adult did not have toString property, then Object.prototype's toString method would be invoked, and we would get "[object Object]" instead of "I'm 8" */ + + /* Note: we have not overridden the child's toString property, thus the + adult's method will be invoked. If adult did not have toString property, then + Object.prototype's toString method would be invoked, and we would get + "[object Object]" instead of "I'm 8" + */ ``` `child`'s prototype is `adult`, whose prototype is `Object.prototype`. This sequence of prototypes is called **prototype chain**. diff --git a/objects/reference.md b/objects/reference.md index 798e1e85..25017beb 100644 --- a/objects/reference.md +++ b/objects/reference.md @@ -3,18 +3,25 @@ Objects are **never copied**. They are passed around by reference. ```js // Imagine I had a pizza var myPizza = {slices: 5}; - // And I shared it with You + + // Then I shared it with you var yourPizza = myPizza; - // I eat another slice + + // I eat a slice myPizza.slices = myPizza.slices - 1; + var numberOfSlicesLeft = yourPizza.slices; // Now We have 4 slices because myPizza and yourPizza - // reference to the same pizza object. + // because they are references to the same pizza object. +``` + +Another example: +```js var a = {}, b = {}, c = {}; // a, b, and c each refer to a // different empty object + a = b = c = {}; // a, b, and c all refer to // the same empty object - ``` diff --git a/strings/README.md b/strings/README.md index 32b92767..1c8c0a4a 100644 --- a/strings/README.md +++ b/strings/README.md @@ -2,7 +2,7 @@ JavaScript strings share many similarities with string implementations from other high-level languages. They represent text based messages and data. -In this course we will cover the basics. How to create new strings and perform common operations on them. +In this course we will cover the basics of how to create new strings and perform common operations on them. Here is an example of a string: diff --git a/strings/length.md b/strings/length.md index b9932177..253b34fb 100644 --- a/strings/length.md +++ b/strings/length.md @@ -1,6 +1,6 @@ # Length -It's easy in Javascript to know how many characters are in string using the property `.length`. +It's easy in Javascript to know how many characters are in a string using the property `.length`. ```js // Just use the property .length