From 5b9daa28ae1b8761fab69aba2248300213d603d1 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Wed, 16 Mar 2022 11:11:22 +0530 Subject: [PATCH 01/17] Update README.md --- README.md | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 9ea5671..e046fc8 100644 --- a/README.md +++ b/README.md @@ -8066,23 +8066,26 @@ c.retrieve(); // => The counter is currently at: 14 ## Q. ***How to divide an array in multiple equal parts in JS?*** ```js -function splitArrayIntoChunksOfLen(arr, len) { - let chunks = [], - i = 0, - n = arr.length; +const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - while (i < n) { - chunks.push(arr.slice(i, (i += len))); +let lenth = 3; + +function split(len) { + while (arr.length > 0) { + let temp = arr.splice(0, len); + console.log(temp); } - return chunks; } -let alphabet = ["a", "b", "c", "d", "e", "f"]; -let alphabetPairs = splitArrayIntoChunksOfLen(alphabet, 2); //split into chunks of two +split(lenth); -console.log(alphabetPairs); +// Output +(3) [1, 2, 3] +(3) [4, 5, 6] +(3) [7, 8, 9] +(1) [10] ``` -**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/splitarrayintochunksoflen-5od3rz?file=/src/index.js:0-345)** +**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/split-array-5od3rz)**
↥ back to top From 52615bd1d6069cac3393e5c87dfbe8f2b859cd8d Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Wed, 16 Mar 2022 11:13:22 +0530 Subject: [PATCH 02/17] Update README.md --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index e046fc8..6f52914 100644 --- a/README.md +++ b/README.md @@ -8067,13 +8067,11 @@ c.retrieve(); // => The counter is currently at: 14 ```js const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - let lenth = 3; function split(len) { while (arr.length > 0) { - let temp = arr.splice(0, len); - console.log(temp); + console.log(arr.splice(0, len)); } } split(lenth); From 38f1bc20ff8137039a9fc32ac02fed62a640c8c9 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Thu, 17 Mar 2022 07:59:22 +0530 Subject: [PATCH 03/17] Update README.md --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6f52914..6bbe8a8 100644 --- a/README.md +++ b/README.md @@ -18,15 +18,13 @@ function sum(x, y) { }; } } -``` - -Output -```js console.log(sum(2,3)); // Outputs 5 console.log(sum(2)(3)); // Outputs 5 ``` +**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-cp-1-ypmjhl?file=/src/index.js)** +
↥ back to top
From b57f69aedae29ef31ab4308412870eb0b5a2e517 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Thu, 17 Mar 2022 08:25:53 +0530 Subject: [PATCH 04/17] Update README.md --- README.md | 88 ++++++++++++++++++++----------------------------------- 1 file changed, 31 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 6bbe8a8..cabdc86 100644 --- a/README.md +++ b/README.md @@ -34,69 +34,43 @@ console.log(sum(2)(3)); // Outputs 5 ```html - - - Show File Data - - - -
- - -
- + file = input.files[0]; + extension = file.name.substring(file.name.lastIndexOf(".") + 1); + + console.log("File Name: " + file.name); + console.log("File Size: " + file.size + " bytes"); + console.log("File Extension: " + extension); + } + + + + +
+ + +
+ + + + +File Name: pic.jpg +File Size: 1159168 bytes +File Extension: jpg ``` +**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-cp-file-upload-fj17kh?file=/index.html)** + From 3ed4e586c2ceef5cacf5ac69e9712d6514f284e9 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Thu, 17 Mar 2022 08:31:41 +0530 Subject: [PATCH 05/17] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cabdc86..1ecd2e4 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ console.log(sum(2)(3)); // Outputs 5 - + Show File Data +

-

- ``` +**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-cp-captcha-mzyi2n?file=/index.html)** + From b3494f2041b7e5573540d85629a5fc571e8a2375 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Thu, 17 Mar 2022 11:11:49 +0530 Subject: [PATCH 09/17] Stopwatch --- README.md | 143 +++++++++++++++++++++--------------------------------- 1 file changed, 55 insertions(+), 88 deletions(-) diff --git a/README.md b/README.md index ef8bade..acf52ce 100644 --- a/README.md +++ b/README.md @@ -111,107 +111,74 @@ File Extension: jpg Stopwatch Example + -
-

Simple stopwatch made in JavaScript

- - - -
-

- 0 : 00 : - 000 -

-

- In this example Date() methods co-operate with timing function - setInterval(). -

+

Time: 00:00:00



+ + + ``` +**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-cp-captcha-mzyi2n?file=/index.html)** + From e3c01946345ad65881a8597966301fe65df2e9c5 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Thu, 17 Mar 2022 11:22:05 +0530 Subject: [PATCH 10/17] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index acf52ce..ae22b42 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,7 @@ File Extension: jpg ``` -**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-cp-captcha-mzyi2n?file=/index.html)** +**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-cp-stopwatch-j6in1i?file=/index.html)**
↥ back to top From 58dbbde1a7d56c4da1191656feaf16b9bd4704b6 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Thu, 17 Mar 2022 11:34:14 +0530 Subject: [PATCH 11/17] Update README.md --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ae22b42..885bbad 100644 --- a/README.md +++ b/README.md @@ -193,9 +193,13 @@ function reverseString(str) { } return stringRev; } -alert(reverseString("Pradeep")); // Output: peedarP +console.log(reverseString("Hello")); + +// Output: olleH ``` +**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-cp-reversestring-sgm1ip?file=/src/index.js)** + @@ -206,8 +210,13 @@ alert(reverseString("Pradeep")); // Output: peedarP function isEmpty(obj) { return Object.keys(obj).length === 0; } + +const obj = {}; +console.log(isEmpty(obj)); // true ``` +**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-cp-isempty-b7n04b?file=/src/index.js)** + ## Q. ***JavaScript Regular Expression to validate Email*** ```javascript From 4763103f0d863cf901608e38da3354f4e1cbaed4 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Thu, 17 Mar 2022 11:43:39 +0530 Subject: [PATCH 12/17] Update README.md --- README.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 885bbad..cd94762 100644 --- a/README.md +++ b/README.md @@ -217,12 +217,27 @@ console.log(isEmpty(obj)); // true **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-cp-isempty-b7n04b?file=/src/index.js)** + + ## Q. ***JavaScript Regular Expression to validate Email*** ```javascript -var pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/; +function validateEmail(email) { + const re = /\S+@\S+\.\S+/; + return re.test(email); +} + +console.log(validateEmail("pradeep.vwa@gmail.com")); // true ``` +**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-cp-validateemail-wfopym?file=/src/index.js)** + + + ## Q. ***Use RegEx to test password strength in JavaScript?*** ```javascript From b718d3aa0da8bae2de56b70223c022281518de70 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Thu, 17 Mar 2022 11:48:52 +0530 Subject: [PATCH 13/17] Update README.md --- README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cd94762..1d1761b 100644 --- a/README.md +++ b/README.md @@ -241,8 +241,8 @@ console.log(validateEmail("pradeep.vwa@gmail.com")); // true ## Q. ***Use RegEx to test password strength in JavaScript?*** ```javascript -var newPassword = "Pq5*@a{J"; -var regularExpression = new RegExp( +let newPassword = "Pq5*@a{J"; +const regularExpression = new RegExp( "^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})" ); @@ -250,7 +250,12 @@ if (!regularExpression.test(newPassword)) { alert( "Password should contain atleast one number and one special character !" ); +} else { + console.log("PASS"); } + +// Output +PASS ``` | RegEx | Description | @@ -262,6 +267,8 @@ if (!regularExpression.test(newPassword)) { | (?=.[!@#\$%\^&]) | The string must contain at least one special character, but we are escaping reserved RegEx characters to avoid conflict | | (?=.{8,}) | The string must be eight characters or longer | +**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-cp-password-strength-cxl8xy)** + From e618210b079444f6fdb1c68a5d3d753dac405a02 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Thu, 14 Apr 2022 10:59:24 +0530 Subject: [PATCH 14/17] Update README.md --- README.md | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/README.md b/README.md index 1d1761b..dc8d626 100644 --- a/README.md +++ b/README.md @@ -8042,3 +8042,62 @@ split(lenth); + +## Q. ***Write a random integers function to print integers with in a range?*** + +**Example:** + +```js +/** + * function to return a random number + * between min and max range + * + * */ +function randomInteger(min, max) { + return Math.floor(Math.random() * (max - min + 1) ) + min; +} +randomInteger(1, 100); // returns a random integer from 1 to 100 +``` + +**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-random-integers-yd1cy8?file=/src/index.js)** + + + +## Q. ***How to convert Decimal to Binary in JavaScript?*** + +**Example 01:** Convert Decimal to Binary + +```js +function DecimalToBinary(number) { + let bin = 0; + let rem, + i = 1; + while (number !== 0) { + rem = number % 2; + number = parseInt(number / 2); + bin = bin + rem * i; + i = i * 10; + } + console.log(`Binary: ${bin}`); +} + +DecimalToBinary(10); +``` + +**Example 02:** Convert Decimal to Binary Using `toString()` + +```js +let val = 10; + +console.log(val.toString(2)); // 1010 ==> Binary Conversion +console.log(val.toString(8)); // 12 ==> Octal Conversion +console.log(val.toString(16)); // A ==> Hexadecimal Conversion +``` + +**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-decimal-to-binary-uhyi8t?file=/src/index.js)** + + From ed357f53dd1741f7e389e67d41674951127d4995 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Fri, 15 Apr 2022 11:57:13 +0530 Subject: [PATCH 15/17] Update README.md --- README.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/README.md b/README.md index dc8d626..d111976 100644 --- a/README.md +++ b/README.md @@ -8101,3 +8101,55 @@ console.log(val.toString(16)); // A ==> Hexadecimal Conversion + +## Q. ***How do you make first letter of the string in an uppercase?*** + +You can create a function which uses chain of string methods such as charAt, toUpperCase and slice methods to generate a string with first letter in uppercase. + +```js +function capitalizeFirstLetter(string) { + let arr = string.split(" "); + for (var i = 0; i < arr.length; i++) { + arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1); + } + return arr.join(" "); +} + +console.log(capitalizeFirstLetter("hello world")); // Hello World +``` + +**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-capitalizefirstletter-dpjhky?file=/src/index.js)** + + + +## Q. ***Write a function which will test string as a literal and as an object?*** + +The `typeof` operator can be use to test string literal and `instanceof` operator to test String object. + +```js +function check(str) { + if (str instanceof String) { + return "It is an object of string"; + } else { + if (typeof str === "string") { + return "It is a string literal"; + } else { + return "another type"; + } + } +} + +var ltrlStr = "Hi I am string literal"; +var objStr = new String("Hi I am string object"); + +console.log(check(ltrlStr)); // It is a string literal +console.log(check(objStr)); // It is an object of string +``` + +**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-literal-vs-object-978dqw?file=/src/index.js)** + + From 67160bb5557b29808ca46a56aea0fc1f5ec8d671 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Thu, 28 Apr 2022 16:33:13 +0530 Subject: [PATCH 16/17] Update README.md --- README.md | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/README.md b/README.md index d111976..918c8ba 100644 --- a/README.md +++ b/README.md @@ -8153,3 +8153,76 @@ console.log(check(objStr)); // It is an object of string + +## Q. ***How do you reversing an array?*** + +You can use reverse() method is used reverse the elements in an array. This method is useful to sort an array in descending order. Let us see the usage of reverse() method in an example, + +```js +let numbers = [1, 2, 5, 3, 4]; +numbers.sort((a, b) => b - a); +numbers.reverse(); +console.log(numbers); // [1, 2, 3, 4 ,5] +``` + + + +## Q. ***How do you find min and max value in an array?*** + +You can use `Math.min` and `Math.max` methods on array variable to find the minimum and maximum elements with in an array. +Let us create two functions to find the min and max value with in an array, + +```js +var marks = [50, 20, 70, 60, 45, 30]; +function findMin(arr) { + return Math.min.apply(null, arr); +} +function findMax(arr) { + return Math.max.apply(null, arr); +} + +console.log(findMin(marks)); +console.log(findMax(marks)); +``` + + + +## Q. ***How do you find min and max values without Math functions?*** + +You can write functions which loops through an array comparing each value with the lowest value or highest value to find the min and max values. Let us create those functions to find min an max values, + +```js +var marks = [50, 20, 70, 60, 45, 30]; +function findMin(arr) { + var length = arr.length + var min = Infinity; + while (length--) { + if (arr[length] < min) { + min = arr[length]; + } + } + return min; +} + +function findMax(arr) { + var length = arr.length + var max = -Infinity; + while (length--) { + if (arr[length] > max) { + max = arr[length]; + } + } + return max; +} + +console.log(findMin(marks)); +console.log(findMax(marks)); +``` + + From 43665e9b5c807dc8f4a5b2490b0ce186c7707645 Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Date: Tue, 17 May 2022 11:10:36 +0530 Subject: [PATCH 17/17] Update README.md --- README.md | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/README.md b/README.md index 918c8ba..113a967 100644 --- a/README.md +++ b/README.md @@ -8226,3 +8226,66 @@ console.log(findMax(marks)); + +## Q. ***Write code for merge two JavaScript Object dynamically?*** + +Let say you have two objects + +```js +const person = { + name: "Tanvi", + age: 28 +}; + +const address = { + addressLine1: "Some Location x", + addressLine2: "Some Location y", + city: "Bangalore" +}; +``` + +Write merge function which will take two object and add all the own property of second object into first object. + +```js +merge(person , address); + +/* Now person should have 5 properties +name , age , addressLine1 , addressLine2 , city */ +``` + +**Method 1: Using ES6, Object.assign method:** + +```js +const merge = (toObj, fromObj) => Object.assign(toObj, fromObj); + +console.log(merge(person, address)); +// {name: "Tanvi", age: 28, addressLine1: "Some Location x", addressLine2: "Some Location y", city: "Bangalore"} +``` + +**Method 2: Without using built-in function:** + +```js +function mergeObject(toObj, fromObj) { + // Make sure both of the parameter is an object + if (typeof toObj === "object" && typeof fromObj === "object") { + for (var pro in fromObj) { + // Assign only own properties not inherited properties + if (fromObj.hasOwnProperty(pro)) { + // Assign property and value + toObj[pro] = fromObj[pro]; + } + } + } else { + throw "Merge function can apply only on object"; + } +} + +console.log(mergeObject(person, address)); +// {name: "Tanvi", age: 28, addressLine1: "Some Location x", addressLine2: "Some Location y", city: "Bangalore"} +``` + +**⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/js-shallow-vs-deep-copy-ik5b7h?file=/src/index.js)** + +