Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 114 additions & 111 deletions JS/README.md
Original file line number Diff line number Diff line change
@@ -1,187 +1,190 @@
# JS CheatSheet
##Comments
###Single Line Comment
> //
## Comments
### Single Line Comment
`// This is a single line comment `

###Multi Line Comment
> /* */
### Multi Line Comment
/* This is a
multi-line comment */

##Variables
> var variable = 1;
## Variables
`var variable = 1;`

##Operators
###Assignment operators
## Operators
### Assignment operators

- x = y : `x = y`
- x = x + y : `x += y`
- x = x - y : `x -= y`
- x = x * y : `x *= y `
- x = x / y : `x /= y`
- `x = y`
- `x += y`, equivalent to `x = x + y`
- `x -= y`, equivalent to `x = x - y`
- `x *= y`, equivalent to `x = x * y`
- `x /= y`, equivalent to `x = x / y`

###Logical operators
### Logical operators

- Equal value and type : `===`
- Not equal value or not equal type : `!==`
- Greater than or equal to : `>=`
- Less than or equal to : `<=`
- Equal value and type: `===`
- Not equal value or not equal type: `!==`
- Greater than or equal to: `>=`
- Less than or equal to: `<=`

####Ternary -- shorthand for a conditional : `?`
> condition ? 'do this if true': 'do this if false'
#### Ternary -- shorthand for a conditional : `?`
`condition ? 'do this if true': 'do this if false'`

Example :
> var canDrink = age > 21 ? 'Go drink!' : 'Sorry you're too young!'
Example: `var canDrink = age > 21 ? 'Go drink!' : 'Sorry you're too young!';`

- Returns true if both are true : `('comparision1' && 'comparison2')`
- Returns true if at least one is true : ` ('comparison1' || 'comparison2') `
- Returns opposite of boolean value : `!('comparison')`

##Arrays
## Arrays

> var arrayName = ['add text', 23, true];
> var arrayName2 = [];
var arrayName = ['add text', 23, true];
var arrayName2 = [];


###Access array items
### Access array items

Item in the ith position of the array `arrayName[i]`


###Add to array
Adds to end, back of the line
> arrayName2.push('what you want to add');
### Add to array
Adds to end, back of the line: `arrayName2.push('what you want to add');`

Adds the front, un is bad - the items are cutting everyone to get to the front
>arrayName2.unshift('new addition', 'another new additon');

###Remove from array
`arrayName2.unshift('new addition', 'another new additon');`

Last item pops off
> arrayName2.pop();
### Remove from array

First item is removed and all others shift up in line
> arrayName2.shift();
Last item pops off: `arrayName2.pop();`

First item is removed and all others shift up in line: `arrayName2.shift();`

###Splicing arrays

> arrayName[i].splice(j,k,x);
### Splicing arrays

`arrayName[i].splice(j,k,x);`

where -

- i is the array index (if you have an array inside an array),
- j is the item index
- k is the number of items to remove
- x is items to add

- `i` is the array index (if you have an array inside an array),
- `j` is the item index
- `k` is the number of items to remove
- `x` is items to add

###Rename item in an array

> arrayName[i] = 'new value'
### Rename item in an array

where i is the index of the item being replaced
`arrayName[i] = 'new value'`

where `i` is the index of the item being replaced

##If Else

<pre>if (condition) {
console.log('do this if true');
} else {
console.log('do this if false');
}</pre>
## If-Else

##For loop
if (condition) {
console.log('do this if true');
} else {
console.log('do this if false');
}

Starting value of i : `i = 0 `
How many times loop will run : `i !==10`
How to increment i after loop, adds 1 to i : `i++`
Will increment by the # specified : `i+=#`
## For loop

for (var i = 0; i !== 10; i++ ) {
console.log(i);
}

<pre>for (var i = 0; i !== 10; i++ ) {
console.log(i);
}</pre>
- Starting value of `i`: `i = 0 `
- How many times loop will run: `i !== 10`
- How to increment `i` after loop, adds 1 to `i`: `i++`
- Will increment by the # specified: `i += #`

##While loop
## While loop

<pre>while (condition) {
console.log('do this');
}</pre>

Will run until condition is false, could run zero times
while (condition) {
console.log('do this');
}

##Do while loop
Will run until condition is false, could run zero times.

<pre>do {
console.log('do something')
} while (condition);</pre>
## Do-while loop

do {
console.log('do something')
} while (condition);

Will run at least once and continues to run until condition is false
Will run at least once and continues to run until condition is false.


##Objects
## Objects

<pre>var objectName = {
property1: "can be text",
property2: ['or', 'an', 'array'],
property3: true,
property4: {
subProperty1: "amanda",
subProperty2: 10,
}
}</pre>
var objectName = {
property1: "can be text",
property2: ['or', 'an', 'array'],
property3: true,
property4: {
subProperty1: "amanda",
subProperty2: 10,
}
}

Access items in an object (dot notation):
>console.log(objectName.property1);
>console.log(objectName.property2[2]);

console.log(objectName.property1);
console.log(objectName.property2[2]);

Object starting with blank
>var newObject = new Object();

<pre>newObject.property1 = "can add text";
newObject.property2 = [1,2,3,4];
newObject.property3 = {
subProperty1: 'new text',
subProperty2: 10,
subProperty3: [2,4,6]
}</pre>
var newObject = new Object();

> console.log(newObject);
newObject.property1 = "can add text";
newObject.property2 = [1,2,3,4];
newObject.property3 = {
subProperty1: 'new text',
subProperty2: 10,
subProperty3: [2,4,6]
}

console.log(newObject);

##Functions

<pre>function functionName(property) {
return property;
}</pre>
## Functions

function functionName(property) {
return property;
}

###Call a function
> functionName(enterProperty);

### Call a function

###Sorting from smallest to largest
`functionName(enterProperty);`

<pre>arrayName.sort(function(a, b) {
return a - b
});</pre>

Change to b - a for largest to smallest
### Sorting from smallest to largest

arrayName.sort(function(a, b) {
return a - b
});

###is NaN
Change to `b - a` for largest to smallest

<pre>function isNaN(testValue) {
return Number.isNaN(Number(testValue));
}</pre>

### is NaN

##Random number generator
function isNaN(testValue) {
return Number.isNaN(Number(testValue));
}


## Random number generator

Generates number between 0 and 1, multiply to get whole numbers
>Math.random()

`Math.random()`

Rounds down to nearest whole number
>Math.floor(number to round down);

`Math.floor(floating_point_number_to_round_down);`

Round to two decimal places
>Math.round(NumberWithMoreDecimals * 100) / 100;

`Math.round(NumberWithMoreDecimals * 100) / 100;`
26 changes: 13 additions & 13 deletions npm-tutorial/README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
#NPM - Node's package manager
# NPM - Node's package manager

##Checking the npm version
## Checking the npm version
In order to check npm's version, type into the node repl:
```
$ npm --version
4.1.2
```

##Installing and upgrading NPM
## Installing and upgrading NPM
npm comes pre - installed with node.
In order to update it, simply type into the node repl:
```
$ npm install npm -g
```

##Searching for npm modules
## Searching for npm modules
```
npm search <module-name>
```
Or, go to [NPM Registry](https://npmjs.com)

##Installing an npm module
## Installing an npm module
```
$ npm install <module-name>
```
Expand All @@ -35,7 +35,7 @@ $ npm install express -g
```
_Note:_ NPM modules (If installed locally) should be installed in the directory of the node instance, or the npm module.

##Uninstall an npm module
## Uninstall an npm module
```
$ npm uninstall <module-name>
```
Expand All @@ -44,7 +44,7 @@ _Example:_
$ npm uninstall express
```

##Updating a module
## Updating a module
```
$ npm update <module-name>
```
Expand All @@ -53,12 +53,12 @@ _Example:_
$ npm update express
```

##Creating your own NPM module
###Creating an account in the NPM registry
## Creating your own NPM module
### Creating an account in the NPM registry
1. Go to [NPM Registry](https://npmjs.com) and create an account
2. Remember the username and password

###Initializing your module
### Initializing your module
1. Create a directory with the name of the module you intend to create; for e.g. vatsal-module-1.
2. Navigate to the folder you've made.
3. Start the node/ npm REPL in the folder.
Expand All @@ -67,7 +67,7 @@ $ npm update express
6. Unless you want to change anything, keep the values default as mention between the parentheses.
7. Done! You've initialized your first node module!

###Writing your module
### Writing your module
+ We will be writing a module which prints the current date and time.
+ After initializing your module, if not done already, navigate to the module's directory.
+ Create a file named index.js.
Expand All @@ -87,11 +87,11 @@ console.log (date.getDate() + "/" + // get Today's Date
(date.getYear - 100));
```

###Running the module
### Running the module
+ Navigate to the directory of your module
+ Type into the node REPL: `$ npm start`

###Publish your module
### Publish your module
+ Change the version number to desired number in package.json. **Do not change anything else**.
+ Make sure you have an account on [NPM Registry](https://npmjs.com).
+ Type into the REPL:
Expand Down
Loading