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
Lines changed: 8 additions & 0 deletions
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('example@example.com')); // true
7+
console.log(validateEmail('example12121@gmail.com')); // true
8+
console.log(validateEmail('example121.gmail.com')); // false

01 - JavaScript-Basics/outputGuess.js

Lines changed: 89 additions & 0 deletions
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();
Lines changed: 46 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

0 commit comments

Comments
 (0)