From df2d2337bf3e8d289c726735dade1cfa3a6904b7 Mon Sep 17 00:00:00 2001 From: Lakshman Date: Sun, 3 May 2020 23:54:16 +0600 Subject: [PATCH 01/56] Create outputGuess.js --- 01 - JavaScript-Basics/outputGuess.js | 89 +++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 01 - JavaScript-Basics/outputGuess.js diff --git a/01 - JavaScript-Basics/outputGuess.js b/01 - JavaScript-Basics/outputGuess.js new file mode 100644 index 0000000..ebd7f00 --- /dev/null +++ b/01 - JavaScript-Basics/outputGuess.js @@ -0,0 +1,89 @@ +'use strict'; +function logThis() { + this.desc = 'logger'; + console.log(this); // { desc: 'logger' } +} + +new logThis(); + +// ''''''''''''''''''''''''''''' +var Storm = function () {}; +Storm.prototype.precip = 'rain'; +var WinterStorm = function () {}; +WinterStorm.prototype = new Storm(); +WinterStorm.prototype.precip = 'snow'; +var bob = new WinterStorm(); +console.log(bob.precip); // snow + +// lllllllllllllllllllllllllllllll +const obj = { + a: 1, + b: 2, + c: 3, +}; + +const obj2 = { + ...obj, + a: 0, +}; + +console.log(obj2.a, obj2.b); // 0 2 + +// llllllllllllllllllllllllllll +console.log(sum(10, 20)); // 30 ReferenceError +// console.log(diff(10, 20)); + function sum(x, y) { + return x + y; + } + let diff = function(x, y) { + return x - y; + } + + + +// lllllllllllllllllllllll +function sayHello() { + console.log("hello"); +} + +var func = sayHello; +func.answer = 42; + +console.log(sayHello.answer); // 42 + + +var a = ['dog', 'cat', 'hen']; +a[100] = 'fox'; + +console.log(a.length); + +var a; +var b = (a=3) ? true: false + +let arr = []; + +console.log(arr); +console.log([] ==[]); + +class X { + get Y() {return 42;} +} + + +var x = new X(); + +console.log(x.Y); + + + +var v = 1; +var f1 = function () { + console.log(v); +} + +var f2 = function () { + var v = 2; + f1(); +} + +f2(); From 77fc917a9f76c75306d51cde8a11b6e1f637453f Mon Sep 17 00:00:00 2001 From: lgope Date: 2020年7月18日 23:52:58 +0600 Subject: [PATCH 02/56] =?UTF-8?q?todays=20day=20name=20in=207=20days=20?= =?UTF-8?q?=F0=9F=91=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01 - JavaScript-Basics/getTodayName.js | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 01 - JavaScript-Basics/getTodayName.js diff --git a/01 - JavaScript-Basics/getTodayName.js b/01 - JavaScript-Basics/getTodayName.js new file mode 100644 index 0000000..bf8d402 --- /dev/null +++ b/01 - JavaScript-Basics/getTodayName.js @@ -0,0 +1,5 @@ +const weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; +// todays day +const day = weekdays[new Date().getDay()]; + +console.log(day); \ No newline at end of file From 8e46e41e02ee719be77d8fa7f82c33f335942c54 Mon Sep 17 00:00:00 2001 From: lgope Date: 2020年8月26日 20:36:37 +0600 Subject: [PATCH 03/56] =?UTF-8?q?recursive=20function=20practice=20?= =?UTF-8?q?=F0=9F=94=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../recursiveFunction.js | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 05 - Objects-And-Functions/recursiveFunction.js diff --git a/05 - Objects-And-Functions/recursiveFunction.js b/05 - Objects-And-Functions/recursiveFunction.js new file mode 100644 index 0000000..46c3f30 --- /dev/null +++ b/05 - Objects-And-Functions/recursiveFunction.js @@ -0,0 +1,45 @@ +// 1 +function countFuction(num) { + console.log(num); + if (num <= 0) { + return; + } + + countFuction(num - 1); +} + +countFuction(8); + +// 2 +function anotherAountFuction(num) { + console.log(num); + if (num> 0) { + anotherAountFuction(num - 1); + } +} + +anotherAountFuction(8); + +// sum of digits +function sumOfDigits(num) { + if (num == 0) { + return 0; + } + return (num % 10) + sumOfDigits(Math.floor(num / 10)); +} + +let sum = sumOfDigits(324); +console.log(`res = ${sum}`); + +// factorial +var factorial = function (number) { + // break condition + if (number <= 0) { + return 1; + } + + // block to execute + return number * factorial(number - 1); +}; + +console.log(`Factorial = ${factorial(9)}`); From e6fed13de3bf9c089dc3e4a296ae4f468aa8c888 Mon Sep 17 00:00:00 2001 From: lgope Date: 2020年8月27日 23:29:53 +0600 Subject: [PATCH 04/56] recursive binary search in js --- algorithms/binarySearchRecursive.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 algorithms/binarySearchRecursive.js diff --git a/algorithms/binarySearchRecursive.js b/algorithms/binarySearchRecursive.js new file mode 100644 index 0000000..026430b --- /dev/null +++ b/algorithms/binarySearchRecursive.js @@ -0,0 +1,19 @@ +function binarySearch(arr, val, start = 0, end = arr.length - 1) { + const mid = Math.floor((start + end) / 2); + + if (val === arr[mid]) { + return mid; + } + + if (start>= end) { + return -1; + } + + return val < arr[mid] + ? binarySearch(arr, val, start, mid - 1) + : binarySearch(arr, val, mid + 1, end); +} + +let arr = [1, 2, 4, 6, 100, 10000]; + +console.log(binarySearch(arr, 2)); From 1caac4b43b84a22d7891a4b1f762ab90a59d26be Mon Sep 17 00:00:00 2001 From: Lakshman Date: 2020年12月12日 20:06:02 +0600 Subject: [PATCH 05/56] Update README.md --- README.md | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bb0c9bf..5f50214 100644 --- a/README.md +++ b/README.md @@ -68,4 +68,51 @@ let str = "Hello world, welcome to the JS Universe."; console.log(str.length); // 40 - ``` \ No newline at end of file + ``` + + +- [1.6](#length) **Interview Qus**: Tricky JavaScript Interview Questions and Answers + + ```javascript +// remove duplicates form and array +let arr = [1, 2, 2, 3, 4]; + +console.log([...new Set(arr)]); + +// output test 1 +console.log(5 < 6 < 7); // true // 5 < 6 => true => true < 7 => true = 1 => 1 < 7 => true + +console.log(7> 6> 5); // false // 7> 6 => true => true> 5 => true = 1 => 1> 5 = false + +console.log(Math.max()); // -Infinity lowest min number in js +console.log(Math.max(1, 2, 3, 4)); // 4 + + +// obj +let profile = { + name: 'Lakshman' +}; + +// Object.freeze(profile); // freeze don't allow insert and update +Object.seal(profile); // freeze don't allow insert, remove but allow update + +profile.name = 'Gope'; + +console.log(profile); + +// obj +let user = { + name: 'Gope' +}; + +// age not allow any update but name does +Object.defineProperty(user, 'age', { + value: 4, + writable: false +}) + +user.name = 'Lakshman' +user.age = 5; + +console.log(user); // TypeError: Cannot assign to read only property 'age' of object '#' + ``` From 70a9b666ab602c53a2024440fa7a8db8d8a00f33 Mon Sep 17 00:00:00 2001 From: Lakshman Date: 2020年12月27日 18:37:01 +0600 Subject: [PATCH 06/56] Create no-loop.js --- js-coding-technique/no-loop.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 js-coding-technique/no-loop.js diff --git a/js-coding-technique/no-loop.js b/js-coding-technique/no-loop.js new file mode 100644 index 0000000..31bbbf9 --- /dev/null +++ b/js-coding-technique/no-loop.js @@ -0,0 +1,12 @@ +// function repeater(x) { +// if (x> 10) return 0; + +// console.log(x++); +// // x++; +// return repeater(x); +// } + +// repeater(1); + +const helloPrint = num => num < 11 && (console.log(num), helloPrint(++num)); +helloPrint(1); From 7bd0e9765269a1ed832c4f1e97f256db6621246a Mon Sep 17 00:00:00 2001 From: Lakshman Date: Sun, 3 Jan 2021 23:30:05 +0600 Subject: [PATCH 07/56] Create arrObj.js --- Interview-Questions/arrObj.js | 117 ++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 Interview-Questions/arrObj.js diff --git a/Interview-Questions/arrObj.js b/Interview-Questions/arrObj.js new file mode 100644 index 0000000..54d7767 --- /dev/null +++ b/Interview-Questions/arrObj.js @@ -0,0 +1,117 @@ +/** + * QUESTION 01 + * OUTPUT + */ + +let obj = { + a: 10, + b: 20 +}; + +let obj2 = { + a: 10, + b: 20 +}; + +console.log(obj == obj2); +console.log(obj === obj2); + +let obj3 = obj; + +console.log(obj3 == obj); +console.log(obj3 === obj); + +/** + * QUESTION 02 + * OUTPUT + */ + +var arr = [1, 2, 3, 4, 5]; + +for (var i = 0; i < arr.length; i++) { + setTimeout(() => console.log(arr[i]), 500); +} + +const arr1 = [1, 2, 3, 4, 5]; + +for (let i = 0; i < arr1.length; i++) { + setTimeout(() => console.log(arr1[i]), 500); +} + +/** + * QUESTION 03 + * OUTPUT + */ + +console.log(1); +setTimeout(() => console.log(2), 0); +console.log(3); +Promise.resolve(console.log(4)); + +/** + * QUESTION 04 + * How to compare two arrays + */ + +let a1 = [1, 2, 3]; +let a2 = [1, 2, 3]; + +console.log(a1 == a2); +console.log(JSON.stringify(a1) == JSON.stringify(a2)); + +/** + * QUESTION 05 + * Calculate total point (using reduce) and avg result + */ + +let EXAM = [ + { + Subeject: "English", + result: { + point: 80, + grade: "A+" + } + }, + { + Subeject: "Physics", + result: { + point: 85, + grade: "A+" + } + }, + { + Subeject: "Math", + result: { + point: 89, + grade: "A+" + } + } +]; + +let totalRes = 0; +EXAM.map(subject => (totalRes += subject.result.point)); + +console.log(totalRes); +console.log((totalRes / EXAM.length).toFixed(2)); + +let res = EXAM.reduce((accumulator, currentValue) => accumulator + currentValue.result.point, 0); +console.log(res); + +/** + * QUESTION 06 + * + * Merge two objects in one object + */ + +let obj1 = { + a: 10, + b: 20 +}; + +let obj2 = { + c: 30, + d: 20 +}; + +obj1 = { ...obj1, ...obj2 }; +console.log(obj1); From cdcaeb6ad6a5593b8565d64c5e1e46ce4ab6a149 Mon Sep 17 00:00:00 2001 From: Lakshman Date: 2021年1月11日 10:42:50 +0600 Subject: [PATCH 08/56] Create removeDuplicates.js --- Array/removeDuplicates.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Array/removeDuplicates.js diff --git a/Array/removeDuplicates.js b/Array/removeDuplicates.js new file mode 100644 index 0000000..fa22f96 --- /dev/null +++ b/Array/removeDuplicates.js @@ -0,0 +1,13 @@ +const fruits = ['🥑', '🍊', '🍇', '🍏', '🍎', '🍑', '🍑']; + +// way 1 +console.log('way 1 ', fruits.reduce((uniqueArray, fruit) => { + uniqueArray.indexOf(fruit) === -1 && uniqueArray.push(fruit); + return uniqueArray; +}, [])) + +// way 2 +console.log('way 2 ', fruits.filter((fruit, index) => fruits.indexOf(fruit) === index)) + +// way 3 +console.log('way 3 ', [...new Set(fruits)]) From 5cb9b01ebe26d89888b704d77b82c72cca3f873a Mon Sep 17 00:00:00 2001 From: Lakshman Date: 2021年1月12日 00:22:33 +0600 Subject: [PATCH 09/56] Update arrObj.js --- Interview-Questions/arrObj.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Interview-Questions/arrObj.js b/Interview-Questions/arrObj.js index 54d7767..b47f5ab 100644 --- a/Interview-Questions/arrObj.js +++ b/Interview-Questions/arrObj.js @@ -115,3 +115,32 @@ let obj2 = { obj1 = { ...obj1, ...obj2 }; console.log(obj1); + + +// more test +const a = [1, 2, 5, 7, 9]; +const b = [2, 5, 7, 12, 100]; + +// const c = [...a, ...b]; + +const c = a.concat(b).sort((a, b) => a> b) + +console.log(c) + + +const obj = { + x: 1, + getX() { + const inner = function() { + console.log(this.x); + } + + inner.bind(this)(); + } +} + +obj.getX(); + +const arrayTotal = a.reduce((t, i) => t+i); + +console.log(arrayTotal) From 894aec530cbc01ca91c3cf7c2d2977a0473275be Mon Sep 17 00:00:00 2001 From: Lakshman Date: 2021年1月14日 16:30:36 +0600 Subject: [PATCH 10/56] Create func.js --- Interview-Questions/func.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Interview-Questions/func.js diff --git a/Interview-Questions/func.js b/Interview-Questions/func.js new file mode 100644 index 0000000..936c234 --- /dev/null +++ b/Interview-Questions/func.js @@ -0,0 +1,11 @@ +// what is output? +const foo = () => { + let a = b = 10; + a++; + return a +} + +foo(); + +console.log(a); +console.log(b); From b5ab2a8373425f516955ff5fd97e7688ebca6163 Mon Sep 17 00:00:00 2001 From: Lakshman Date: 2021年1月19日 23:34:25 +0600 Subject: [PATCH 11/56] Update binarySearchRecursive.js --- algorithms/binarySearchRecursive.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/algorithms/binarySearchRecursive.js b/algorithms/binarySearchRecursive.js index 026430b..349b6d9 100644 --- a/algorithms/binarySearchRecursive.js +++ b/algorithms/binarySearchRecursive.js @@ -1,19 +1,17 @@ -function binarySearch(arr, val, start = 0, end = arr.length - 1) { +const binarySearch = (arr, val, start = 0, end = arr.length - 1) => { const mid = Math.floor((start + end) / 2); - if (val === arr[mid]) { - return mid; - } + if (val === arr[mid]) return mid; - if (start>= end) { - return -1; - } + if (start>= end) return -1; return val < arr[mid] ? binarySearch(arr, val, start, mid - 1) : binarySearch(arr, val, mid + 1, end); -} +}; -let arr = [1, 2, 4, 6, 100, 10000]; +const arr = [1, 9, 5, 7, 2, 4, 8, 6].sort(); -console.log(binarySearch(arr, 2)); +const result = binarySearch(arr, 2); + +console.log(result !== -1 ? `Element is present at index ${result}` : 'Element is not present in array'); From 928480b9e30bdcce1acd45192fd060f784883d30 Mon Sep 17 00:00:00 2001 From: Lakshman Date: 2021年1月20日 13:28:33 +0600 Subject: [PATCH 12/56] Create bubbleSortRecursive.js --- algorithms/bubbleSortRecursive.js | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 algorithms/bubbleSortRecursive.js diff --git a/algorithms/bubbleSortRecursive.js b/algorithms/bubbleSortRecursive.js new file mode 100644 index 0000000..ef024b6 --- /dev/null +++ b/algorithms/bubbleSortRecursive.js @@ -0,0 +1,34 @@ +function bubbleSort(arr, iteration) { + if (iteration === 0) return; + + bubbleSwap(arr, 0, 1, iteration); + bubbleSort(arr, iteration - 1); +} + + +function bubbleSwap(arr, i, j, n) { + + if (j>= n) return; + + // swap + if (arr[i]> arr[j]) [arr[i], arr[j]] = [arr[j], arr[i]]; + + + bubbleSwap(arr, i + 1, j + 1, n); +} + + +function sort(arr) { + const n = arr.length; + + if (n <= 1) return; + + bubbleSort(arr, n); + +} + +const arr = [1, 8, 6, 5, 4, 9]; + +sort(arr); + +console.log(arr) From 099355e3f93d6ed961007c4b149e6c0fa0acd8ff Mon Sep 17 00:00:00 2001 From: Lakshman Date: 2021年1月20日 15:18:52 +0600 Subject: [PATCH 13/56] Update bubbleSortRecursive.js --- algorithms/bubbleSortRecursive.js | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/algorithms/bubbleSortRecursive.js b/algorithms/bubbleSortRecursive.js index ef024b6..47100a9 100644 --- a/algorithms/bubbleSortRecursive.js +++ b/algorithms/bubbleSortRecursive.js @@ -7,28 +7,18 @@ function bubbleSort(arr, iteration) { function bubbleSwap(arr, i, j, n) { - if (j>= n) return; // swap if (arr[i]> arr[j]) [arr[i], arr[j]] = [arr[j], arr[i]]; - bubbleSwap(arr, i + 1, j + 1, n); } - -function sort(arr) { - const n = arr.length; - - if (n <= 1) return; - - bubbleSort(arr, n); - -} - const arr = [1, 8, 6, 5, 4, 9]; -sort(arr); +if (arr.length>= 1) bubbleSort(arr, arr.length) console.log(arr) + +// tc => O(n^2) From fe96b7a38d2919851bf249c9bf9ee6ff483b802b Mon Sep 17 00:00:00 2001 From: Lakshman Date: 2021年1月22日 16:03:51 +0600 Subject: [PATCH 14/56] Update arrObj.js --- Interview-Questions/arrObj.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Interview-Questions/arrObj.js b/Interview-Questions/arrObj.js index b47f5ab..4ecf07e 100644 --- a/Interview-Questions/arrObj.js +++ b/Interview-Questions/arrObj.js @@ -144,3 +144,11 @@ obj.getX(); const arrayTotal = a.reduce((t, i) => t+i); console.log(arrayTotal) + + +// OUTPUT +const arr = [1, 2, 3, 4, 5]; + +arr.push(arr.push(arr.push(arr.pop()))) + +console.log(arr); From f3e68adb2f682d4fbfff510494455a13686c44f5 Mon Sep 17 00:00:00 2001 From: Lakshman Date: 2021年2月15日 12:01:32 +0600 Subject: [PATCH 15/56] Create searchAnyValue.js --- 05 - Objects-And-Functions/searchAnyValue.js | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 05 - Objects-And-Functions/searchAnyValue.js diff --git a/05 - Objects-And-Functions/searchAnyValue.js b/05 - Objects-And-Functions/searchAnyValue.js new file mode 100644 index 0000000..043eb4f --- /dev/null +++ b/05 - Objects-And-Functions/searchAnyValue.js @@ -0,0 +1,22 @@ +const arr = [{ + name: 'xyz', + grade: 'xs' +}, { + name: 'yaya', + grade: 'xa' +}, { + name: 'xf', + frade: 'dd' +}, { + name: 'a', + grade: 'b' +}]; + + +function filterIt(arr, searchKey) { + return arr.filter(obj => Object.keys(obj).some(key => obj[key].includes(searchKey))); +} + +console.log("find 'x'", filterIt(arr,"x")); +console.log("find 'a'", filterIt(arr,"a")); +console.log("find 'z'", filterIt(arr,"z")); From 8f6a336da409778c47161944aa85e5c3f8af0d7b Mon Sep 17 00:00:00 2001 From: Lakshman Date: 2021年2月23日 10:35:58 +0600 Subject: [PATCH 16/56] Update arrObj.js --- Interview-Questions/arrObj.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Interview-Questions/arrObj.js b/Interview-Questions/arrObj.js index 4ecf07e..5b0a7d8 100644 --- a/Interview-Questions/arrObj.js +++ b/Interview-Questions/arrObj.js @@ -152,3 +152,10 @@ const arr = [1, 2, 3, 4, 5]; arr.push(arr.push(arr.push(arr.pop()))) console.log(arr); + + +// OUTPUT +const arrayOfOddNumbers = [1, 3, 5]; +arrayOfOddNumbers[100] = 199; +console.log(arrayOfOddNumbers.length); + From 708e2a298f201254c53ed6cab91b77b4c8463788 Mon Sep 17 00:00:00 2001 From: Lakshman Date: Wed, 3 Mar 2021 10:38:48 +0600 Subject: [PATCH 17/56] Create arrayFalsyBouncer.js --- js-coding-technique/arrayFalsyBouncer.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 js-coding-technique/arrayFalsyBouncer.js diff --git a/js-coding-technique/arrayFalsyBouncer.js b/js-coding-technique/arrayFalsyBouncer.js new file mode 100644 index 0000000..7312ffa --- /dev/null +++ b/js-coding-technique/arrayFalsyBouncer.js @@ -0,0 +1,9 @@ +const evenNumberSquared = [1,2,3,4].map(n => { + if (n % 2 !== 0) { + return null; + } + + return n * n; +}).filter(Boolean); + +console.log(evenNumberSquared) From be2860682ba812f0597c5864cfc911c98aebe80d Mon Sep 17 00:00:00 2001 From: Lakshman Date: 2021年3月15日 16:13:42 +0600 Subject: [PATCH 18/56] Create toFixed.js --- BuildIn-Methods/toFixed.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 BuildIn-Methods/toFixed.js diff --git a/BuildIn-Methods/toFixed.js b/BuildIn-Methods/toFixed.js new file mode 100644 index 0000000..e42fd92 --- /dev/null +++ b/BuildIn-Methods/toFixed.js @@ -0,0 +1,10 @@ +const fixNum = (num, n = 2) => Number.parseFloat(num).toFixed(n); + +console.log(fixNum(123.456)); +// expected output: "123.46" + +console.log(fixNum(0.004)); +// expected output: "0.00" + +console.log(fixNum('1.23e+5', 4)); +// expected output: "123000.0000" From 087105501a2ae58d55e155cfdedfe597982d6661 Mon Sep 17 00:00:00 2001 From: Lakshman Date: 2021年3月18日 16:21:40 +0600 Subject: [PATCH 19/56] Create validURL.js --- Regex/validURL.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Regex/validURL.js diff --git a/Regex/validURL.js b/Regex/validURL.js new file mode 100644 index 0000000..b923707 --- /dev/null +++ b/Regex/validURL.js @@ -0,0 +1,11 @@ +const validURL = (str) => { + const pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol + '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name + '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address + '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path + '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string + '(\\#[-a-z\\d_]*)?$','i'); // fragment locator + return !!pattern.test(str); +} + +console.log(validURL("https://www.google.com/")); From 88b4884d5c017dce47e665c2d1e52d154ea4e86a Mon Sep 17 00:00:00 2001 From: Lakshman Date: 2021年4月24日 22:08:22 +0600 Subject: [PATCH 20/56] Update func.js --- Interview-Questions/func.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Interview-Questions/func.js b/Interview-Questions/func.js index 936c234..e9d4bcb 100644 --- a/Interview-Questions/func.js +++ b/Interview-Questions/func.js @@ -9,3 +9,28 @@ foo(); console.log(a); console.log(b); + + +// output +function createIncrement() { + let count = 0; + + function increment() { + count++; + } + + let message = `Count is ${count}`; + function log() { + console.log(message); + } + + return [increment, log]; +} + +const [increment, log] = createIncrement(); + +increment(); +increment(); +increment(); + +log(); From 4fc48fc989242204411a579728384e6939a387b4 Mon Sep 17 00:00:00 2001 From: Lakshman Date: 2021年5月13日 22:16:04 +0600 Subject: [PATCH 21/56] Update byeTryCatchErrorHandling.js --- .../byeTryCatchErrorHandling.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Asynchronous-JavaScript/byeTryCatchErrorHandling.js b/Asynchronous-JavaScript/byeTryCatchErrorHandling.js index 7a37ab2..e8360f5 100644 --- a/Asynchronous-JavaScript/byeTryCatchErrorHandling.js +++ b/Asynchronous-JavaScript/byeTryCatchErrorHandling.js @@ -44,3 +44,23 @@ exports.createOne = Model => }, }); }); + +// another + +const awaitHandlerFactory = (middleware) => { + return async (req, res, next) => { + try { + await middleware(req, res, next); + } catch (err) { + next(err); + } + }; +}; +// and use it this way: +app.get( + "/", + awaitHandlerFactory(async (request, response) => { + const result = await getContent(); + response.send(result); + }) +); From ec4f9d963a5f56d45fc32de11b476819852d7616 Mon Sep 17 00:00:00 2001 From: Lakshman Date: Wed, 4 Aug 2021 16:02:50 +0600 Subject: [PATCH 22/56] Create renameDuplicates.js --- js-coding-technique/renameDuplicates.js | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 js-coding-technique/renameDuplicates.js diff --git a/js-coding-technique/renameDuplicates.js b/js-coding-technique/renameDuplicates.js new file mode 100644 index 0000000..7981800 --- /dev/null +++ b/js-coding-technique/renameDuplicates.js @@ -0,0 +1,47 @@ +// 1 +var array = [ +{ + name: "Steven Smith", + Country: "England", + Age: 35 +}, +{ + name: "Hannah Reed", + Country: "Scottland", + Age: 23 +}, +{ + name: "Steven Smith", + Country: "Spain", + Age: 35 +}, +]; + + let names = {}; + arr.forEach(obj => { + if (names[obj.name]) { + obj.name += "_" + ++names[obj.name]; + } else { + names[obj.name] = 1; + } + }); + + +// 2 + function renameFiles(arr) { + let count = {}; + arr.forEach((x, i) => { + if (arr.indexOf(x) !== i) { + let c = x in count ? (count[x] = count[x] + 1) : (count[x] = 1); + let j = c + 1; + let k = `${x }(${ j })`; + + while (arr.indexOf(k) !== -1) k = `${x }(${ ++j })`; + arr[i] = k; + } + }); + return arr; + } + + let res = renameFiles(['a(1)', 'a(6)', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']); + console.log(res); From 5384212719abc81dd56f9a21a8f628148907024d Mon Sep 17 00:00:00 2001 From: Lakshman Date: Fri, 6 Aug 2021 16:29:35 +0600 Subject: [PATCH 23/56] Update renameDuplicates.js --- js-coding-technique/renameDuplicates.js | 131 ++++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/js-coding-technique/renameDuplicates.js b/js-coding-technique/renameDuplicates.js index 7981800..f035b78 100644 --- a/js-coding-technique/renameDuplicates.js +++ b/js-coding-technique/renameDuplicates.js @@ -45,3 +45,134 @@ var array = [ let res = renameFiles(['a(1)', 'a(6)', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']); console.log(res); + + +// Other should update +function renameFiles(arrObj, renameField='contents') { + let arr = []; + if (renameField === 'contents') { + arrObj.forEach((child) => { + arr.push(child.properties[renameField]); + }); + } else { + arrObj.forEach((child) => { + arr.push(child.properties.attributes[renameField]); + }); + } + + let count = {}; + arr.forEach((x, i) => { + if (arr.indexOf(x) !== i) { + let c = x in count ? (count[x] = count[x] + 1) : (count[x] = 1); + let j = c + 1; + let k = `${x} (${j})`; + + while (arr.indexOf(k) !== -1) k = `${x} (${++j})`; + arr[i] = k; + } + }); + + for (let i = 0; i < arrObj.length; i++) { + arrObj[i].properties.contents = arr[i]; + } + + return arrObj; +} + +// let res = renameFiles(['a(1)', 'a(6)', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']); +let res = renameFiles([ + { + id: 1, + properties: { contents: 'a' }, + }, + { + id: 2, + properties: { contents: 'a' }, + }, + { + id: 3, + properties: { contents: 'a' }, + }, + { + id: 1, + properties: { contents: 'a' }, + }, + { + id: 1, + properties: { contents: 'a (3)' }, + }, +]); +console.log(res); + +// function renameDuplicates(arr) { +// let count = {}; +// arr.forEach((x, i) => { +// if (arr.indexOf(x) !== i) { +// console.log(arr.indexOf(x), i); +// let c = x in count ? (count[x] = count[x] + 1) : (count[x] = 1); +// let j = c + 1; +// let k = `${x}(${j})`; + +// while (arr.indexOf(k) !== -1) k = `${x}(${++j})`; +// arr[i] = k; +// } +// }); +// return arr; +// } + +// let res1 = renameDuplicates(['a(1)', 'a(6)', 'a(1)', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']); +// console.log(res1); + +const renameDuplicates = (childrens) => { + // console.log({ inputChildrens: childrens }); + let temp = childrens.length; + + childrens = JSON.parse(JSON.stringify(childrens)); + let count = {}; + childrens.forEach((child) => { + if (count[child.properties.contents]) { + child.properties.contents += ` (${++count[child.properties.contents]})`; + while (temp) { + temp--; + if (childrens[temp].properties.contents === child.properties.contents || count[child.properties.contents]) + child.properties.contents += ` (${1})`; + } + } else { + count[child.properties.contents] = 1; + } + + // if (count[child.properties.attributes.value]) { + // child.properties.attributes.value += ` (${++count[child.properties.attributes.value]})`; + // } else { + // count[child.properties.attributes.value] = 1; + // } + }); + + return childrens; +}; + +const res1 = renameDuplicates([ + { + id: 1, + properties: { contents: 'a' }, + }, + { + id: 2, + properties: { contents: 'a' }, + }, + { + id: 3, + properties: { contents: 'a' }, + }, + { + id: 1, + properties: { contents: 'a (2)' }, + }, + { + id: 1, + properties: { contents: 'a (3)' }, + }, +]); + +// console.log(res1); + From bb878d22bc7c8660d6bb634aafa205f5392286b7 Mon Sep 17 00:00:00 2001 From: Lakshman Date: Wed, 1 Sep 2021 11:40:57 +0600 Subject: [PATCH 24/56] Create removeEmptyKeys.js --- js-coding-technique/removeEmptyKeys.js | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 js-coding-technique/removeEmptyKeys.js diff --git a/js-coding-technique/removeEmptyKeys.js b/js-coding-technique/removeEmptyKeys.js new file mode 100644 index 0000000..87f5037 --- /dev/null +++ b/js-coding-technique/removeEmptyKeys.js @@ -0,0 +1,5 @@ +export const removeEmptyKeys = (obj) => + Object.entries(obj).reduce( + (accumulator, [key, value]) => (value ? ((accumulator[key] = value), accumulator) : accumulator), + {}, + ); From d758c4151a26d876f490581531e068a1da8a8569 Mon Sep 17 00:00:00 2001 From: Lakshman Date: Fri, 8 Oct 2021 11:25:12 +0600 Subject: [PATCH 25/56] Create getTextTotalLine.js --- js-coding-technique/getTextTotalLine.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 js-coding-technique/getTextTotalLine.js diff --git a/js-coding-technique/getTextTotalLine.js b/js-coding-technique/getTextTotalLine.js new file mode 100644 index 0000000..cb63790 --- /dev/null +++ b/js-coding-technique/getTextTotalLine.js @@ -0,0 +1,24 @@ +const getTextTotalLine = (el) => { + let divHeight = 1; + if (el.tagName === 'TEXTAREA') { + divHeight = el.scrollHeight; + } else { + divHeight = el.offsetHeight; + } + let lineHeight = window + .getComputedStyle(el, null) + .getPropertyValue('line-height'); + lineHeight = parseFloat(lineHeight.split('px')[0]); + let totalLine = Math.ceil(divHeight / lineHeight); + return totalLine; + } + +const onChange = () => { + const totalLine = getTextTotalLine(anchorEl.current); +} + +return ( +
+