Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 399 of 840
Load a text file and find number of characters in the file - JavaScript
Suppose we have a data.txt file that lives in the same directory as our NodeJS file. Suppose the content of that file is − Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s. We are required to write a ...
Read MoreGenerating n random numbers between a range - JavaScript
We are required to write a JavaScript function that takes in a number, say n, and an array of two numbers that represents a range. The function should return an array of n random elements all lying between the range provided by the second argument. Understanding the Problem The challenge is to generate unique random numbers within a specified range. We need two helper functions: one to generate a single random number between two values, and another to collect n unique random numbers. Example Implementation Following is the code − const num = 10; ...
Read MoreClearing localStorage in JavaScript?
localStorage is a web storage API that persists data in the browser until explicitly cleared. Here are two effective methods to clear localStorage in JavaScript. Method 1: Using clear() Method The clear() method removes all key-value pairs from localStorage at once. This is the most efficient approach. // Store some data first localStorage.setItem("name", "John"); localStorage.setItem("age", "25"); localStorage.setItem("city", "New York"); console.log("Before clearing:", localStorage.length); // Clear all localStorage localStorage.clear(); console.log("After clearing:", localStorage.length); Before clearing: 3 After clearing: 0 Method 2: ...
Read MoreSleep in JavaScript delay between actions?
To create a sleep or delay in JavaScript, use setTimeout() for simple delays or async/await with Promises for more control. JavaScript doesn't have a built-in sleep function like other languages. 1000 milliseconds = 1 second 2000 milliseconds = 2 seconds, etc. Using setTimeout() for Delays setTimeout() executes a function after a specified delay. Here's an example with a 3-second delay: console.log("Starting calculation..."); setTimeout(function() { var firstValue = 10; var secondValue = 20; var result = firstValue + secondValue; ...
Read MoreCount number of factors of a number - JavaScript
We are required to write a JavaScript function that takes in a number and returns the count of numbers that exactly divides the input number. For example − If the number is 12, then its factors are − 1, 2, 3, 4, 6, 12 Therefore, the output should be 6. Method 1: Basic Approach This method checks every number from 1 to the given number to see if it divides evenly: function countFactorsBasic(num) { let count = 0; for (let i = 1; i { let count = 0; let flag = 2; while (flag
Read MoreHow to subtract elements of two arrays and store the result as a positive array in JavaScript?
Suppose, we have two arrays like these − const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [9, 8, 7, 5, 8, 3]; We are required to write a JavaScript function that takes in two such arrays and returns an array of absolute difference between the corresponding elements of the array. Therefore, for these arrays, the output should look like − const output = [8, 6, 4, 1, 3, 3]; We will use a for loop and keep pushing the absolute difference iteratively into a new array and ...
Read MoreJavaScript global Property
The global property in JavaScript returns true or false depending on whether the 'g' (global) modifier is set in a regular expression pattern. Syntax regexPattern.global Return Value Returns a boolean value: true - if the 'g' flag is set false - if the 'g' flag is not set Example: Checking Global Flag JavaScript Global Property JavaScript Global Property Example ...
Read MoreCombine objects and delete a property with JavaScript
We have the following array of objects that contains two objects and we are required to combine both objects into one and get rid of the chk property altogether: const err = [ { "chk" : true, "name": "test" }, { "chk" : true, "post": "test" } ]; ...
Read MoreExtract key value from a nested object in JavaScript?
Extracting keys from nested objects is a common requirement in JavaScript. This tutorial shows how to find the parent object and nested property that contains a specific value. Creating a Nested Object Let's start with a nested object containing teacher and subject information: var details = { "teacherDetails": { "teacherName": ["John", "David"] }, "subjectDetails": { "subjectName": ["MongoDB", "Java"] } } console.log("Nested object created:", ...
Read MoreSort object array based on another array of keys - JavaScript
When working with arrays of objects in JavaScript, you may need to sort one array based on the order defined in another array. This is particularly useful when you have a reference array that defines the desired sorting order. Problem Statement Suppose we have two arrays like these: const arr1 = ['d', 'a', 'b', 'c']; const arr2 = [{a:1}, {c:3}, {d:4}, {b:2}]; console.log("Original arrays:"); console.log("arr1:", arr1); console.log("arr2:", arr2); Original arrays: arr1: [ 'd', 'a', 'b', 'c' ] arr2: [ { a: 1 }, { c: 3 }, { d: 4 }, { ...
Read More