Skip to content

Commit d71a276

Browse files
committed
Merge branch 'master' of https://github.com/lgope/JavaScript
2 parents 77fc917 + c1235a2 commit d71a276

File tree

13 files changed

+380
-1
lines changed

13 files changed

+380
-1
lines changed
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function validateEmail(email) {
2+
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
3+
return re.test(String(email).toLowerCase());
4+
}
5+
6+
console.log(validateEmail('[email protected]')); // true
7+
console.log(validateEmail('[email protected]')); // true
8+
console.log(validateEmail('example121.gmail.com')); // false

01 - JavaScript-Basics/outputGuess.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
'use strict';
2+
function logThis() {
3+
this.desc = 'logger';
4+
console.log(this); // { desc: 'logger' }
5+
}
6+
7+
new logThis();
8+
9+
// '''''''''''''''''''''''''''''
10+
var Storm = function () {};
11+
Storm.prototype.precip = 'rain';
12+
var WinterStorm = function () {};
13+
WinterStorm.prototype = new Storm();
14+
WinterStorm.prototype.precip = 'snow';
15+
var bob = new WinterStorm();
16+
console.log(bob.precip); // snow
17+
18+
// lllllllllllllllllllllllllllllll
19+
const obj = {
20+
a: 1,
21+
b: 2,
22+
c: 3,
23+
};
24+
25+
const obj2 = {
26+
...obj,
27+
a: 0,
28+
};
29+
30+
console.log(obj2.a, obj2.b); // 0 2
31+
32+
// llllllllllllllllllllllllllll
33+
console.log(sum(10, 20)); // 30 ReferenceError
34+
// console.log(diff(10, 20));
35+
function sum(x, y) {
36+
return x + y;
37+
}
38+
let diff = function(x, y) {
39+
return x - y;
40+
}
41+
42+
43+
44+
// lllllllllllllllllllllll
45+
function sayHello() {
46+
console.log("hello");
47+
}
48+
49+
var func = sayHello;
50+
func.answer = 42;
51+
52+
console.log(sayHello.answer); // 42
53+
54+
55+
var a = ['dog', 'cat', 'hen'];
56+
a[100] = 'fox';
57+
58+
console.log(a.length);
59+
60+
var a;
61+
var b = (a=3) ? true: false
62+
63+
let arr = [];
64+
65+
console.log(arr);
66+
console.log([] ==[]);
67+
68+
class X {
69+
get Y() {return 42;}
70+
}
71+
72+
73+
var x = new X();
74+
75+
console.log(x.Y);
76+
77+
78+
79+
var v = 1;
80+
var f1 = function () {
81+
console.log(v);
82+
}
83+
84+
var f2 = function () {
85+
var v = 2;
86+
f1();
87+
}
88+
89+
f2();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// catchAwait.js
2+
const catchAwait = promise =>
3+
promise
4+
.then(data => ({ data, error: null }))
5+
.catch(error => ({ error, data: null }));
6+
7+
module.exports = catchAwait;
8+
9+
// working file
10+
const { getItems } = require('./api/items');
11+
const catchAwait = require('./utils/catchAwait');
12+
13+
const allItems = async () => {
14+
const { error, data } = await catchAwait(getItems());
15+
if (!error) {
16+
// code
17+
}
18+
console.error(error);
19+
};
20+
21+
allItems();
22+
23+
/**
24+
* Another way
25+
*/
26+
27+
// catchAsync.js
28+
module.exports = fn => {
29+
return (req, res, next) => {
30+
fn(req, res, next).catch(next);
31+
};
32+
};
33+
34+
// createOne.js
35+
36+
exports.createOne = Model =>
37+
catchAsync(async (req, res, next) => {
38+
const doc = await Model.create(req.body);
39+
40+
res.status(201).json({
41+
status: 'success',
42+
data: {
43+
data: doc,
44+
},
45+
});
46+
});

BuildIn-Methods/indexOf.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let str = 'Hello world, welcome to the JS Universe.';
2+
console.log(str.indexOf('welcome')); // 13
3+
console.log(str.indexOf('wall')); // -1
4+
5+
const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon'];
6+
console.log(fruits.indexOf('Melon')); // 3
7+
8+
console.log(fruits.indexOf('klkljkh')); // -1

BuildIn-Methods/lastIndexOf.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon'];
2+
console.log(fruits.lastIndexOf('Melon')); // 3
3+
4+
console.log(fruits.lastIndexOf('klkljkh')); // -1

BuildIn-Methods/length.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon'];
2+
console.log(fruits.length); // 4
3+
4+
let str = 'Hello world, welcome to the JS Universe.';
5+
console.log(str.length); // 40

BuildIn-Methods/map.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
2+
3+
const array = [1, 4, 9, 16, 15, 48];
4+
5+
// pass a function to map
6+
const map1 = array.map(ar => ar * 2);
7+
8+
console.log(map1);
9+
// expected output: Array [2, 8, 18, 32]
10+
11+
function myFunction(num) {
12+
return num * 10;
13+
}
14+
15+
let newArray = array.map(myFunction);
16+
console.log(newArray);

BuildIn-Methods/reduce.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// The reduce() method executes a reducer function (that we provide) on each element of the array, resulting in single output value.
2+
3+
const array = [1, 2, 3, 4, 5];
4+
const reducerFunc = (accumulator, currentValue) => accumulator + currentValue;
5+
6+
// 1 + 2 + 3 + 4 + 5
7+
console.log(array.reduce(reducerFunc));
8+
// expected output: 15
9+
10+
// 5 + 1 + 2 + 3 + 4 + 5
11+
console.log(array.reduce(reducerFunc, 5));
12+
// expected output: 20

BuildIn-Methods/slice.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included) where begin and end represent the index of items in that array. The original array will not be modified.
2+
3+
const array = [1, 2, 3, 4, 5];
4+
5+
const anotherArr = array.slice(1, 4);
6+
7+
console.log(anotherArr);
8+
9+
console.log(array.slice(2, 5));

BuildIn-Methods/toString.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
let num = 10;
2+
let n = num.toString();
3+
4+
console.log(typeof num); // number
5+
6+
console.log(typeof n); // string

BuildIn-Methods/typeOf.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
console.log(typeof 44); // number
2+
3+
console.log(typeof 'something'); // string
4+
5+
console.log(typeof true); // boolean
6+
7+
let num = 12;
8+
console.log(typeof num); // number

README.md

+70-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,71 @@
11
# JavaScript
2-
The of this repo is to save my js programs. Basics of JavaScript. Beginner level.
2+
*JavaScript, often abbreviated as JS, is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm. It has curly-bracket syntax, dynamic typing, prototype-based object-orientation, and first-class functions. The of this repo is to save my js programs. Basics of JavaScript. Beginner level.*
3+
4+
## Table of Contents
5+
6+
1. [Important Methods](#methods)
7+
8+
## Methods
9+
> Most important javascript build in methods
10+
11+
<a name="typeof"></a><a name="1.1"></a>
12+
- [1.1](#typeof) **typeof**: Returns the type.
13+
14+
```javascript
15+
console.log(typeof 44); // number
16+
17+
console.log(typeof 'something'); // string
18+
19+
console.log(typeof true); // boolean
20+
21+
let num = 12;
22+
console.log(typeof(num)); // number
23+
24+
```
25+
26+
<a name="toString"></a><a name="1.2"></a>
27+
- [1.2](#toString) **toString**: Returns the string representation of the number's value.
28+
29+
```javascript
30+
let num = 10;
31+
let n = num.toString();
32+
33+
console.log(typeof(num)); // number
34+
35+
console.log(typeof(n)); // string
36+
```
37+
38+
<a name="indexOf"></a><a name="1.3"></a>
39+
- [1.3](#indexOf) **indexOf**: Returns the first index at which a given element can be found in the array, or -1 if it is not present.
40+
41+
```javascript
42+
let str = "Hello world, welcome to the JS Universe.";
43+
console.log(str.indexOf("welcome")); // 13
44+
console.log(str.indexOf("wall")); // -1
45+
46+
const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon'];
47+
console.log(fruits.indexOf('Melon')); // 3
48+
49+
console.log(fruits.indexOf('klkljkh')); // -1
50+
```
51+
52+
<a name="lastIndexOf"></a><a name="1.4"></a>
53+
- [1.4](#lastIndexOf) **lastIndexOf**: Returns the last index at which a given element can be found in the array, or -1 if it is not present.
54+
55+
```javascript
56+
const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon'];
57+
console.log(fruits.lastIndexOf('Melon')); // 3
58+
59+
console.log(fruits.lastIndexOf('klkljkh')); // -1
60+
```
61+
62+
<a name="length"></a><a name="1.5"></a>
63+
- [1.5](#length) **length**: Returns the number of characters or size in a string or array.
64+
65+
```javascript
66+
const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon'];
67+
console.log(fruits.length); // 4
68+
69+
let str = "Hello world, welcome to the JS Universe.";
70+
console.log(str.length); // 40
71+
```

js-coding-technique/loops.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
const mil = 1000000;
2+
const arr = Array(mil);
3+
4+
console.time('⏲️');
5+
6+
for (let i = 0; i < mil; i++) {} // 1.6ms
7+
8+
for (const v of arr) {
9+
} // 11.7ms
10+
11+
arr.forEach(v => v); // 2.1ms
12+
13+
for (let i = mil; i > 0; i--) {} // 1.5ms
14+
15+
console.timeEnd('⏲️');
16+
17+
const equine = { unicorn: '🦄', horse: '🐴', zebra: '🦓' };
18+
19+
for (const key in equine) {
20+
// Filters out properties inherited from prototype
21+
if (equine.hasOwnProperty(key)) {
22+
console.log(equine[key]);
23+
}
24+
}
25+
26+
// Unwrap the the Values
27+
28+
for (const val of Object.values(equine)) {
29+
console.log(val);
30+
}
31+
32+
// Create a Map
33+
const equine = new Map(Object.entries(equine));
34+
35+
for (const v of equine.values()) {
36+
console.log(v);
37+
}
38+
39+
const equine = [
40+
['unicorn', '🦄'],
41+
['horse', '🐴'],
42+
['zebra', '🦓'],
43+
];
44+
45+
// 😒 Meh Code
46+
for (const arr of equine) {
47+
const type = arr[0];
48+
const face = arr[1];
49+
console.log(`${type} looks like ${face}`);
50+
}
51+
52+
// 🤯 Destructured Code
53+
for (const [type, face] of equine) {
54+
console.log(`${type} looks like ${face}`);
55+
}
56+
57+
///////////////
58+
arr[Symbol.iterator] = function () {
59+
let i = 0;
60+
let arr = this;
61+
return {
62+
next: function () {
63+
if (i >= arr.length) {
64+
return { done: true };
65+
} else {
66+
const value = arr[i] + '🙉';
67+
i++;
68+
return { value, done: false };
69+
}
70+
},
71+
};
72+
};
73+
74+
////////////////////////////////
75+
76+
const faces = ['😀', '😍', '🤤', '🤯', '💩', '🤠', '🥳'];
77+
78+
// Transform values
79+
const withIndex = faces.map((v, i) => `face ${i} is ${v}`);
80+
81+
// Test at least one value meets a condition
82+
const isPoopy = faces.some(v => v === '💩');
83+
// false
84+
85+
// Test all values meet a condition
86+
const isEmoji = faces.every(v => v > 'ÿ');
87+
// true
88+
89+
// Filter out values
90+
const withoutPoo = faces.filter(v => v !== '💩');
91+
92+
// Reduce values to a single value
93+
const pooCount = faces.reduce((acc, cur) => {
94+
return acc + (cur === '💩' ? 1 : 0);
95+
}, 0);
96+
console.log(pooCount);
97+
98+
// Sort the values
99+
const sorted = faces.sort((a, b) => a < b);

0 commit comments

Comments
 (0)