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 441 of 840
Create a Calculator function in JavaScript
We have to write a function, say calculator() that takes in one of the four characters (+, - , *, / ) as the first argument and any number of Number literals after that. Our job is to perform the operation specified as the first argument over those numbers and return the result. If the operation is multiplication or addition, we are required to perform the same operation with every element. But if the operation is subtraction or division, we have to consider the first element as neutral and subtract all other elements from it or divide it by ...
Read MoreRegex - reusing patterns to capture groups in JavaScript?
Regular expressions can use backreferences to capture groups and reuse them later in the pattern. The syntax \1, \2, etc., refers to previously captured groups. How Backreferences Work When you create a group with parentheses (), the regex engine remembers the matched content. You can reference this captured content later using \1 for the first group, \2 for the second, and so on. Syntax /^(pattern1)(pattern2)\1\2$/ // \1 matches the same text as the first group // \2 matches the same text as the second group Example: Matching Repeated Patterns var groupValues1 ...
Read MoreRandom name generator function in JavaScript
We are required to write a JavaScript function that takes in a number n and returns a random string of length n containing no other than the 26 English lowercase alphabets. Example Let us write the code for this function: const num = 8; const randomNameGenerator = num => { let res = ''; for(let i = 0; i < num; i++){ const random = Math.floor(Math.random() * 26); res += String.fromCharCode(97 + ...
Read MoreStrict equality vs Loose equality in JavaScript.
In JavaScript, there are two ways to compare values for equality: loose equality (==) and strict equality (===). Understanding the difference is crucial for writing reliable code. Loose Equality (==) The loose equality operator == compares values after converting them to a common type (type coercion). This can lead to unexpected results. Loose Equality Examples Loose Equality Results ...
Read MoreFilter away object in array with null values JavaScript
When working with arrays of objects, you often need to filter out objects with invalid or empty values. This is common when processing API responses or user data that may contain incomplete records. Let's say we have an array of employee objects, but some have empty strings, null, or undefined values for the name field. We need to filter out these invalid entries. Sample Data Here's our employee data with some invalid entries: let data = [{ "name": "Ramesh Dhiman", "age": 67, "experience": ...
Read MoreChecking the intensity of shuffle of an array - JavaScript
An array of numbers is 100% shuffled if no two consecutive numbers appear together in ascending order. It is 0% shuffled if all adjacent pairs are in ascending order. The shuffle intensity measures how much an array deviates from a perfectly sorted ascending sequence. For an array of length n, there are n-1 adjacent pairs to examine. We calculate the percentage of pairs that are NOT in ascending order. How It Works The algorithm counts pairs where the first element is greater than the second element (descending pairs). The shuffle intensity is calculated as: Shuffle ...
Read MoreAccessing an array returned by a function in JavaScript
In JavaScript, functions can return arrays that can be accessed and manipulated directly. This is useful for creating reusable code that generates data structures. Basic Syntax function functionName() { let array = [/* elements */]; return array; } // Access the returned array let result = functionName(); Example: Returning and Accessing Arrays Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...
Read MoreStrictly increasing sequence JavaScript
Given a sequence of integers as an array, we have to determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. For example: For sequence = [1, 3, 2, 1], the output should be function(sequence) = false. There is no one element in this array that can be removed in order to get a strictly increasing sequence. For sequence = [1, 3, 2], the output should be function(sequence) = true. You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, ...
Read MoreNot able to push all elements of a stack into another stack using for loop in JavaScript?
When transferring elements from one stack to another using a for loop, you need to understand that the stack follows the Last In First Out (LIFO) principle. The key is to pop() elements from the source stack and push() them into the destination stack. The Stack Transfer Process When you pop elements from the first stack and push them into the second stack, the order gets reversed because of the LIFO nature: var myFirstStack = [10, 20, 30, 40, 50, 60, 70]; var mySecondStack = []; console.log("Original first stack:", myFirstStack); console.log("Original second stack:", mySecondStack); ...
Read MoreNearest Prime to a number - JavaScript
We are required to write a JavaScript function that takes in a number and returns the first prime number that appears after n. For example: If the number is 24, then the output should be 29. Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples include 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, etc. Implementation We'll create two functions: one to check if a number is prime, and another to find the nearest prime after a given ...
Read More