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 295 of 840
Counting number of 9s encountered while counting up to n in JavaScript
We need to write a JavaScript function that counts how many times the digit "9" appears when counting from 0 to a given number n. For example, counting from 0 to 100 includes numbers like 9, 19, 29, 90, 91, 99, etc., where "9" appears multiple times. Problem We are required to write a JavaScript function that takes in a number n. Our function should count and return the number of times we will have to use the digit 9 while counting from 0 to n. Example Following is the code − const num ...
Read MoreCounting steps to make number palindrome in JavaScript
We are required to write a JavaScript function that takes in a number, num, as the first and the only argument. Our function should return the number of special steps needed to obtain a palindrome. The special step is: "reverse the digits, and add to the original number". If the resulting number is not a palindrome, repeat the procedure with the sum until the resulting number is a palindrome. Problem Example For example, if the input to the function is: const num = 87; The expected output is 4 steps because: ...
Read MoreFinding shortest word in a string in JavaScript
We are required to write a JavaScript function that takes in a string and returns the shortest word from the string. For example: If the input string is: const str = 'This is a sample string'; Then the output should be: 'a' Using Array.reduce() Method This approach splits the string into words and uses reduce() to find the shortest word by comparing lengths: const str = 'This is a sample string'; const findSmallest = str => { const strArr = str.split(' '); ...
Read MoreSearch by id and remove object from JSON array in JavaScript
Suppose, we have an array of objects that contains data about some movies like this − const arr = [ {id: "1", name: "Snatch", type: "crime"}, {id: "2", name: "Witches of Eastwick", type: "comedy"}, {id: "3", name: "X-Men", type: "action"}, {id: "4", name: "Ordinary People", type: "drama"}, {id: "5", name: "Billy Elliot", type: "drama"}, {id: "6", name: "Toy Story", type: "children"} ]; We are required to write ...
Read MoreLongest decreasing subsequence subarray in JavaScript
We are required to write a JavaScript function that takes in an array of integers and returns the length of the longest consecutive decreasing subsequence (subarray) from the array. Problem Statement Given an array of integers, find the length of the longest contiguous subsequence where each element is smaller than the previous one. For example, if the input array is: const arr = [5, 2, 5, 4, 3, 2, 4, 6, 7]; The longest decreasing subsequence is [5, 4, 3, 2] with length 4. Solution Approach We'll track the current decreasing ...
Read MoreAdding and searching for words in custom Data Structure in JavaScript
We are required to design a data structure in JavaScript that supports two key operations: adding words and searching with pattern matching using regular expressions. Problem The data structure must support the following operations: addWord - adds a word to the data structure using arrays or any other storage mechanism search - searches for a literal word or a regular expression pattern containing lowercase letters "a-z" or "." where "." can represent any single letter Example Requirements addWord("sir") addWord("car") addWord("mad") search("hell") === false search(".ad") === true search("s..") === true ...
Read MoreRemoving parentheses from mathematical expressions in JavaScript
When working with mathematical expressions in JavaScript, you may need to remove parentheses while preserving the correct operations and operands. This involves carefully tracking signs and handling nested expressions. Problem We need to write a JavaScript function that takes a string of mathematical expressions and removes parentheses while keeping operations and operands in their correct positions with proper signs. For example, if the input is: const str = 'u-(v-w-(x+y))-z'; The expected output should be: u-v+w+x+y-z How It Works The algorithm uses a stack to track sign changes when ...
Read MoreJavaScript: Sort Object of Objects
Suppose we have an Object of Objects like this: const obj = { "CAB": { name: 'CBSSP', position: 2 }, "NSG": { name: 'NNSSP', position: 3 }, "EQU": { name: 'SSP', position: 1 } }; We need to write a JavaScript function that sorts the sub-objects based on the 'position' property in ascending order. Method 1: Using Object.keys() and ...
Read MoreFuzzy Search Algorithm in JavaScript
Fuzzy search allows finding strings that match a pattern even when characters are not consecutive. In JavaScript, we can implement a fuzzy search algorithm that checks if characters from a search query appear in the same order within a target string. How Fuzzy Search Works The algorithm loops through each character of the search query and verifies that all characters exist in the target string in the same sequential order, though they don't need to be adjacent. For example: ('a haystack with a needle').fuzzySearch('hay sucks'); // false ('a haystack with a needle').fuzzySearch('sack hand'); // true ...
Read MoreLongest possible string built from two strings in JavaScript
We need to write a JavaScript function that takes two strings containing only letters from a to z and returns the longest possible sorted string with distinct letters from both strings combined. Problem Statement Given two strings s1 and s2, create a function that: Combines both strings Removes duplicate characters Returns a sorted string containing each unique letter only once Solution Using Array Methods Here's an implementation that combines the strings, removes duplicates, and sorts the result: const str1 = "xyaabbbccccdefww"; const str2 = "xxxxyyyyabklmopq"; const longestPossible = (str1 = '', ...
Read More