-
Notifications
You must be signed in to change notification settings - Fork 4
added 5 important buil in functions .. #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cb3c83c
added 5 important buil in functions ..
lgope 00477c9
Create byeTryCatchErrorHandling.js
lgope e653a4f
loop tips
lgope df7677b
Merge branch 'before-marster' of https://github.com/lgope/JavaScript ...
lgope 024b9e1
async await error handling without try catch 🚀
lgope File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
Asynchronous-JavaScript/byeTryCatchErrorHandling.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // catchAwait.js | ||
| const catchAwait = promise => | ||
| promise | ||
| .then(data => ({ data, error: null })) | ||
| .catch(error => ({ error, data: null })); | ||
|
|
||
| module.exports = catchAwait; | ||
|
|
||
| // working file | ||
| const { getItems } = require('./api/items'); | ||
| const catchAwait = require('./utils/catchAwait'); | ||
|
|
||
| const allItems = async () => { | ||
| const { error, data } = await catchAwait(getItems()); | ||
| if (!error) { | ||
| // code | ||
| } | ||
| console.error(error); | ||
| }; | ||
|
|
||
| allItems(); | ||
|
|
||
| /** | ||
| * Another way | ||
| */ | ||
|
|
||
| // catchAsync.js | ||
| module.exports = fn => { | ||
| return (req, res, next) => { | ||
| fn(req, res, next).catch(next); | ||
| }; | ||
| }; | ||
|
|
||
| // createOne.js | ||
|
|
||
| exports.createOne = Model => | ||
| catchAsync(async (req, res, next) => { | ||
| const doc = await Model.create(req.body); | ||
|
|
||
| res.status(201).json({ | ||
| status: 'success', | ||
| data: { | ||
| data: doc, | ||
| }, | ||
| }); | ||
| }); | ||
lgope marked this conversation as resolved.
Show resolved
Hide resolved
|
||
71 changes: 70 additions & 1 deletion
README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,71 @@ | ||
| # JavaScript | ||
| The of this repo is to save my js programs. Basics of JavaScript. Beginner level. | ||
| *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.* | ||
|
|
||
| ## Table of Contents | ||
|
|
||
| 1. [Important Methods](#methods) | ||
|
|
||
| ## Methods | ||
| > Most important javascript build in methods | ||
|
|
||
| <a name="typeof"></a><a name="1.1"></a> | ||
| - [1.1](#typeof) **typeof**: Returns the type. | ||
|
|
||
| ```javascript | ||
| console.log(typeof 44); // number | ||
|
|
||
| console.log(typeof 'something'); // string | ||
|
|
||
| console.log(typeof true); // boolean | ||
|
|
||
| let num = 12; | ||
| console.log(typeof(num)); // number | ||
|
|
||
| ``` | ||
|
|
||
| <a name="toString"></a><a name="1.2"></a> | ||
| - [1.2](#toString) **toString**: Returns the string representation of the number's value. | ||
|
|
||
| ```javascript | ||
| let num = 10; | ||
| let n = num.toString(); | ||
|
|
||
| console.log(typeof(num)); // number | ||
|
|
||
| console.log(typeof(n)); // string | ||
| ``` | ||
|
|
||
| <a name="indexOf"></a><a name="1.3"></a> | ||
| - [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. | ||
|
|
||
| ```javascript | ||
| let str = "Hello world, welcome to the JS Universe."; | ||
| console.log(str.indexOf("welcome")); // 13 | ||
| console.log(str.indexOf("wall")); // -1 | ||
|
|
||
| const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon']; | ||
| console.log(fruits.indexOf('Melon')); // 3 | ||
|
|
||
| console.log(fruits.indexOf('klkljkh')); // -1 | ||
| ``` | ||
|
|
||
| <a name="lastIndexOf"></a><a name="1.4"></a> | ||
| - [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. | ||
|
|
||
| ```javascript | ||
| const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon']; | ||
| console.log(fruits.lastIndexOf('Melon')); // 3 | ||
|
|
||
| console.log(fruits.lastIndexOf('klkljkh')); // -1 | ||
| ``` | ||
|
|
||
| <a name="length"></a><a name="1.5"></a> | ||
| - [1.5](#length) **length**: Returns the number of characters or size in a string or array. | ||
|
|
||
| ```javascript | ||
| const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon']; | ||
| console.log(fruits.length); // 4 | ||
|
|
||
| let str = "Hello world, welcome to the JS Universe."; | ||
| console.log(str.length); // 40 | ||
| ``` |
99 changes: 99 additions & 0 deletions
js-coding-technique/loops.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| const mil = 1000000; | ||
| const arr = Array(mil); | ||
|
|
||
| console.time('⏲️'); | ||
|
|
||
| for (let i = 0; i < mil; i++) {} // 1.6ms | ||
|
|
||
| for (const v of arr) { | ||
| } // 11.7ms | ||
|
|
||
| arr.forEach(v => v); // 2.1ms | ||
|
|
||
| for (let i = mil; i > 0; i--) {} // 1.5ms | ||
|
|
||
| console.timeEnd('⏲️'); | ||
|
|
||
| const equine = { unicorn: '🦄', horse: '🐴', zebra: '🦓' }; | ||
|
|
||
| for (const key in equine) { | ||
| // Filters out properties inherited from prototype | ||
| if (equine.hasOwnProperty(key)) { | ||
| console.log(equine[key]); | ||
| } | ||
| } | ||
|
|
||
| // Unwrap the the Values | ||
|
|
||
| for (const val of Object.values(equine)) { | ||
| console.log(val); | ||
| } | ||
|
|
||
| // Create a Map | ||
| const equine = new Map(Object.entries(equine)); | ||
|
|
||
| for (const v of equine.values()) { | ||
| console.log(v); | ||
| } | ||
|
|
||
| const equine = [ | ||
| ['unicorn', '🦄'], | ||
| ['horse', '🐴'], | ||
| ['zebra', '🦓'], | ||
| ]; | ||
|
|
||
| // 😒 Meh Code | ||
| for (const arr of equine) { | ||
| const type = arr[0]; | ||
| const face = arr[1]; | ||
| console.log(`${type} looks like ${face}`); | ||
| } | ||
|
|
||
| // 🤯 Destructured Code | ||
| for (const [type, face] of equine) { | ||
| console.log(`${type} looks like ${face}`); | ||
| } | ||
|
|
||
| /////////////// | ||
| arr[Symbol.iterator] = function () { | ||
| let i = 0; | ||
| let arr = this; | ||
| return { | ||
| next: function () { | ||
| if (i >= arr.length) { | ||
| return { done: true }; | ||
| } else { | ||
| const value = arr[i] + '🙉'; | ||
| i++; | ||
| return { value, done: false }; | ||
| } | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| //////////////////////////////// | ||
|
|
||
| const faces = ['😀', '😍', '🤤', '🤯', '💩', '🤠', '🥳']; | ||
|
|
||
| // Transform values | ||
| const withIndex = faces.map((v, i) => `face ${i} is ${v}`); | ||
|
|
||
| // Test at least one value meets a condition | ||
| const isPoopy = faces.some(v => v === '💩'); | ||
| // false | ||
|
|
||
| // Test all values meet a condition | ||
| const isEmoji = faces.every(v => v > 'ÿ'); | ||
| // true | ||
|
|
||
| // Filter out values | ||
| const withoutPoo = faces.filter(v => v !== '💩'); | ||
|
|
||
| // Reduce values to a single value | ||
| const pooCount = faces.reduce((acc, cur) => { | ||
| return acc + (cur === '💩' ? 1 : 0); | ||
| }, 0); | ||
| console.log(pooCount); | ||
|
|
||
| // Sort the values | ||
| const sorted = faces.sort((a, b) => a < b); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.