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 298 of 840
Find all occurrences of a word in array in JavaScript
When working with arrays in JavaScript, you might need to find how many elements contain a specific word or substring. This article demonstrates multiple approaches to count occurrences of a word within array elements. Problem Statement We need to write a JavaScript function that takes an array of strings as the first argument and a search word as the second argument. The function should return the count of array elements that contain the specified word. Method 1: Using filter() and indexOf() This approach uses filter() to find matching elements and returns the length of the filtered ...
Read MoreInsert a number into a sorted array of numbers JavaScript
We are required to write a JavaScript function that takes in a sorted array of numbers as the first argument and a single number as the second argument. The function should push the number specified as the second argument into the array without distorting the sorting of the elements. We are required to do this without creating another array. Approach: Binary Search with In-Place Insertion The solution uses binary search to find the correct insertion position, then shifts elements using a swapping technique to maintain order without extra space. Example const arr = ...
Read MoreFinding minimum number of required operations to reach n from m in JavaScript
We are required to write a JavaScript function that takes in two numbers, m and n, as the first and the second argument. Our function is supposed to count the number of minimum operations required to reach n from m, using only these two operations: Double − Multiply the number on the display by 2, or; Decrement − Subtract 1 from the number on the display. ...
Read MoreHow can a particular frame be targeted, from a hyperlink, in JavaScript?
HTML frames provide a convenient way to divide a browser window into multiple sections, where each section can load a separate HTML document. JavaScript allows you to target and manipulate specific frames using several methods through the window.frames property and DOM manipulation. The frames property is an array-like object containing all the frames (including iframes) on the current page. Here are the main approaches to target a particular frame: Method 1: Using Frame Index You can target a specific frame by its index position in the frames collection. The first frame has index 0: // ...
Read MoreCheck if a string is entirely made of the same substring JavaScript
In JavaScript, you can check if a string is entirely made of repeated substrings using various approaches. This is useful for pattern validation and string analysis. The problem requires that the string consists of a repeated character sequence with at least one repetition. For example, "aa" contains two "a" substrings, "abcabcabc" contains three "abc" substrings, but "ababa" fails because it has an extra character. Examples of Valid and Invalid Patterns "aa" should return true because it entirely contains two strings "a" "aaa" should return true because it entirely ...
Read MorePrime digits sum of a number in JavaScript
We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should then sum all the digits of the number that are prime and return the sum as a number. For example − If the input number is − const num = 67867852; Then the output should be − const output = 21; because 7 + 7 + 5 + 2 = 21 − Understanding Prime Digits Prime digits are single-digit prime numbers: 2, 3, 5, and ...
Read MoreChecking validity of equations in JavaScript
In JavaScript, we need to check if a set of variable equations can all be satisfied simultaneously. This involves parsing equality and inequality constraints and determining if consistent variable assignments exist. Problem Statement Given an array of string equations in the format 'X===Y' (equality) or 'X!==Y' (inequality), determine if we can assign values to variables such that all equations evaluate to true. Understanding the Logic The solution uses a Union-Find (Disjoint Set) data structure: Group equal variables into the same set Check if any inequality constraint violates the grouping If variables that must be ...
Read MoreCounting possible APs within an array in JavaScript
Arithmetic Progression (AP) is a sequence of numbers where the difference between any two consecutive numbers is constant (called the common difference). For instance, 1, 2, 3, 4, 5, 6, … is an AP with a common difference of 1 (2 - 1 = 1). Problem Statement We need to write a JavaScript function that takes a sorted array of integers and returns the count of arithmetic progressions of size 3 that can be formed from the array elements. In each progression, the differences between consecutive elements must be the same. The input array is guaranteed ...
Read MoreKeeping only redundant words in a string in JavaScript
We are required to write a JavaScript function that takes in a string and returns a new string with only the words that appeared more than once in the original string. Problem Statement If the input string is: const str = 'this is a is this string that contains that some repeating words'; Then the output should be: 'this is that' The function should identify duplicate words and return them in the order they first appeared. Using indexOf() and lastIndexOf() This approach checks if a word's first occurrence ...
Read MoreInverting slashes in a string in JavaScript
When working with strings in JavaScript, you may need to replace forward slashes (/) with backslashes (\) or vice versa. This is common when dealing with file paths or URL manipulation. This article demonstrates how to create a function that takes a string containing forward slashes and converts them to backslashes. Problem Statement We need to write a JavaScript function that: Takes a string that may contain forward slashes (/) Returns a new string with all forward slashes replaced by backslashes (\) Method 1: Using a for Loop Here's a solution using a ...
Read More