fix(guide): simplify directory structure

This commit is contained in:
Mrugesh Mohapatra
2018-10-16 21:26:13 +05:30
parent f989c28c52
commit da0df12ab7
35752 changed files with 0 additions and 317652 deletions

View File

@ -0,0 +1,46 @@
---
title: Break Statement
---
## Introduction
The **break** statement terminates the current loop, `switch` or `label` statement and transfers program control to the statement following the terminated statement.
break;
If the **break** statement is used in a labeled statement, the syntax is as follows:
break labelName;
## Examples
The following function has a **break** statement that terminates the `while` loop when **i** is 3, and then returns the value **3 * x**.
function testBreak(x) {
var i = 0;
while (i < 6) {
if (i == 3) {
break;
}
i += 1;
}
return i * x;
}
![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":rocket:") <a href='https://repl.it/C7VM/0' target='_blank' rel='nofollow'>Run Code</a>
In the following example, the counter is set up to count from 1 to 99; however, the **break** statement terminates the loop after 14 counts.
for (var i = 1; i < 100; i++) {
if (i == 15) {
break;
}
}
![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":rocket:") <a href='https://repl.it/C7VO/0' target='_blank' rel='nofollow'>Run Code</a>
## Other resources:
<a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break' target='_blank' rel='nofollow'>MDN link</a> | <a href='https://msdn.microsoft.com/en-us/library/3fhdxafb.aspx' target='_blank' rel='nofollow'>MSDN link</a>

View File

@ -0,0 +1,53 @@
---
title: Continue Statement
---
## Introduction
The **continue** statement terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.
continue;
If the **continue** statement is used in a labeled statement, the syntax is as follows:
continue labelName;
In contrast to the **break** statement, **continue** does not terminate the execution of the loop entirely; instead:
- In a `while` loop, it jumps back to the condition.
- In a `for` loop, it jumps to the update expression.
## Examples
The following example shows a `while` loop that has a **continue** statement that executes when the value of **i** is 3\. Thus, **n** takes on the values 1, 3, 7, and 12.
var i = 0;
var n = 0;
while (i < 5) {
i++;
if (i === 3) {
continue;
}
n += i;
console.log (n);
}
![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":rocket:") <a href='https://repl.it/C7hx/0' target='_blank' rel='nofollow'>Run Code</a>
In the following example, a loop iterates from 1 through 9\. The statements between **continue** and the end of the `for` body are skipped because of the use of the **continue** statement together with the expression `(i < 5)`.
for (var i = 1; i < 10; i++) {
if (i < 5) {
continue;
}
console.log (i);
}
![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":rocket:") <a href='https://repl.it/C7hs/0' target='_blank' rel='nofollow'>Run Code</a>
## Other Resources
* [MDN link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue)
* [MSDN link](https://msdn.microsoft.com/en-us/library/8de3fkc8.aspx)

View File

@ -0,0 +1,35 @@
---
title: Do...While Loop
---
The `do...while` loop is closely related to <a href='http://forum.freecodecamp.com/t/javascript-while-loop/14668' target='_blank' rel='nofollow'>`while`</a> loop. In the do while loop, the condition is checked at the end of the loop.
Here is the **syntax** for `do...while` loop:
## Syntax:
do {
*Statement(s);*
} while (*condition*);
**statement(s):** A statement that is executed **at least once** before the condition or Boolean expression is evaluated and is re-executed each time the condition evaluates to true.
**condition:** Here, a condition is a <a>Boolean expression</a>. If Boolean expression evaluates to true, the statement is executed again. When Boolean expression evaluates to false, the loops ends.
## Example:
var i = 0;
do {
i = i + 1;
console.log(i);
} while (i < 5);
Output:
1
2
3
4
5
source: <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while' target='_blank' rel='nofollow'>**do...while**</a>

View File

@ -0,0 +1,71 @@
---
title: For...In Loop
---
The `for...in` statement iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.
for (variable in object) {
...
}
| Required/Optional | Parameter | Description |
|-------------------|-----------|----------------------------------------------------------------------|
| Required | Variable | A different property name is assigned to variable on each iteration. |
| Optional | Object | Object whose enumerable properties are iterated. |
## Examples
// Initialize object.
a = { "a": "Athens", "b": "Belgrade", "c": "Cairo" }
// Iterate over the properties.
var s = ""
for (var key in a) {
s += key + ": " + a[key];
s += "<br />";
}
document.write (s);
// Output:
// a: Athens
// b: Belgrade
// c: Cairo
// Initialize the array.
var arr = new Array("zero", "one", "two");
// Add a few expando properties to the array.
arr["orange"] = "fruit";
arr["carrot"] = "vegetable";
// Iterate over the properties and elements.
var s = "";
for (var key in arr) {
s += key + ": " + arr[key];
s += "<br />";
}
document.write (s);
// Output:
// 0: zero
// 1: one
// 2: two
// orange: fruit
// carrot: vegetable
// Efficient way of getting an object's keys using an expression within the for-in loop's conditions
var myObj = {a: 1, b: 2, c:3}, myKeys = [], i=0;
for (myKeys[i++] in myObj);
document.write(myKeys);
//Output:
// a
// b
// c
# Ohter Resources:
* [MDN link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in)
* [MSDN link](https://msdn.microsoft.com/library/55wb2d34.aspx)

View File

@ -0,0 +1,84 @@
---
title: For Loop
---
### Syntax
```javascript
for ([initialization]); [condition]; [final-expression]) {
// statement
}
```
The javascript `for` statement consists of three expressions and a statement:
## Description
* initialization - Run before the first execution on the loop. This expression is commonly used to create counters. Variables created here are scoped to the loop. Once the loop has finished it's execution they are destroyed.
* condition - Expression that is checked prior to the execution of every iteration. If omitted, this expression evaluates to true. If it evaluates to true, the loop's statement is executed. If it evaluates to false, the loop stops.
* final-expression - Expression that is run after every iteration. Usually used to increment a counter. But it can be used to decrement a counter too.
* statement - Code to be repeated in the loop
any of these three expressions or the statement can be omitted. For loops are commonly used to count a certain number of iterations to repeat a statement. Use a `break` statement to exit the loop before the condition expression evaluates to false.
## Common Pitfalls
**Exceeding the bounds of an array**
When indexing over an array many times it is easy to exceed the bounds of the array (ex. try to reference the 4th element of a 3 element array).
```javascript
// This will cause an error.
// The bounds of the array will be exceeded.
var arr = [ 1, 2, 3 ];
for (var i = 0; i <= arr.length; i++) {
console.log(arr[i]);
}
output:
1
2
3
undefined
```
There are two ways to fix this code. Set the condition to either `i < arr.length` or `i <= arr.length - 1`
### Examples
Iterate through integers from 0-8
```javascript
for (var i = 0; i < 9; i++) {
console.log(i);
}
output:
0
1
2
3
4
5
6
7
8
```
Break out of a loop before condition expression is false
```javascript
for (var elephant = 1; elephant < 10; elephant+=2) {
if (elephant === 7) {
break;
}
console.info('elephant is ' + elephant);
}
output:
elephant is 1
elephant is 3
elephant is 5
```
### Other Resources
* [MDN - for statements](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for)

View File

@ -0,0 +1,86 @@
---
title: For...Of Loop
---
The `for...of` statement creates a loop iterating over iterable objects (including Array, Map, Set, Arguments object and so on), invoking a custom iteration hook with statements to be executed for the value of each distinct property.
```javascript
for (variable of object) {
statement
}
```
| | Description |
|----------|-------------------------------------|
| variable | On each iteration a value of a different property is assigned to variable. |
| object | Object whose enumerable properties are iterated. |
## Examples
### Array
```javascript
let arr = [ "fred", "tom", "bob" ];
for (let i of arr) {
console.log(i);
}
// Output:
// fred
// tom
// bob
```
### Map
```javascript
var m = new Map();
m.set(1, "black");
m.set(2, "red");
for (var n of m) {
console.log(n);
}
// Output:
// 1,black
// 2,red
```
### Set
```javascript
var s = new Set();
s.add(1);
s.add("red");
for (var n of s) {
console.log(n);
}
// Output:
// 1
// red
```
### Arguments object
```javascript
// your browser must support for..of loop
// and let-scoped variables in for loops
function displayArgumentsObject() {
for (let n of arguments) {
console.log(n);
}
}
displayArgumentsObject(1, 'red');
// Output:
// 1
// red
```
# Other Resources:
* [MDN link](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...of)
* [MSDN link](https://msdn.microsoft.com/library/dn858238%28v=vs.94%29.aspx?f=255&MSPPError=-2147217396)
* [arguments @@iterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/@@iterator)

View File

@ -0,0 +1,14 @@
---
title: Loops
---
Loops are used in JavaScript to perform repeated tasks based on a condition. Conditions typically return `true` or `false` when analysed. A loop will continue running until the defined condition returns `false`.
There are three common types of loops:
* <a href='http://forum.freecodecamp.com/t/javascript-for-loop/14666' target='_blank' rel='nofollow'>for</a>
* <a href='http://forum.freecodecamp.com/t/javascript-while-loop/14668' target='_blank' rel='nofollow'>while</a>
* <a href='http://forum.freecodecamp.com/t/javascript-for-loop/14662' target='_blank' rel='nofollow'>do while</a>
You can type `js for`, `js while` or `js do while` to get more info on any of these.
> Links: <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for' target='_blank' rel='nofollow'>MDN **for loop**</a>

View File

@ -0,0 +1,79 @@
---
title: Labeled Statement
---
## Labeled Statement
The **Labeled Statement** is used with the `break` and `continue` statements and serves to identify the statement to which the `break` and `continue` statements apply.
### Syntax
``` javascript
labelname:
statements
```
### Usage
Without the use of a `labeled` statement the `break` statement can only break out of a loop or a `switch` statement. Using a `labeled` statement allows `break` to jump out of any code block.
#### Example
``` javascript
foo: {
console.log("This prints:");
break foo;
console.log("This will never print.");
}
console.log("Because execution jumps to here!")
/* output
This prints:
Because execution jumps to here! */
```
When used with a `continue` statement the `labeled` statement allows you to skip a loop iteration, the advantage comes from being able to jump out from an inner loop to an outer one when you have nested loop statements. Without the use of a `labeled` statement you could only jump out of the existing loop iteration to the `next iteration of the same loop.`
#### Example
``` javascript
// without labeled statement, when j==i inner loop jumps to next iteration
function test() {
for (var i = 0; i < 3; i++) {
console.log("i=" + i);
for (var j = 0; j < 3; j++) {
if (j === i) {
continue;
}
console.log("j=" + j);
}
}
}
/* output
i=0 (note j=0 is missing)
j=1
j=2
i=1
j=0 (note j=1 is missing)
j=2
i=2
j=0
j=1 (note j=2 is missing)
*/
// using a labeled statement we can jump to the outer (i) loop instead
function test() {
outer: for (var i = 0; i < 3; i++) {
console.log("i=" + i);
for (var j = 0; j < 3; j++) {
if (j === i) {
continue outer;
}
console.log("j=" + j);
}
}
}
/*
i=0 (j only logged when less than i)
i=1
j=0
i=2
j=0
j=1
*/
```
### More Information:
<a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label' target='_blank' rel='nofollow'>MDN</a>

View File

@ -0,0 +1,42 @@
---
title: While Loop
---
The while loop starts by evaluating the condition. If the condition is true, the statement(s) is/are executed. If the condition is false, the statement(s) is/are not executed. After that, while loop ends.
Here is the **syntax** for while loop:
## Syntax:
while (condition)
{
statement(s);
}
_statement(s):_ A statement that is executed as long as the condition evaluates to true.
_condition:_ Here, condition is a Boolean expression which is evaluated before each pass through the loop. If this condition evaluates to true, statement(s) is/are executed. When condition evaluates to false, execution continues with the statement after the while loop.
## Example:
var i = 1;
while (i < 10)
{
console.log(i);
i++; // i=i+1 same thing
}
Output:
1
2
3
4
5
6
7
8
9
*Source: [While Loop - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)*