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 375 of 840
Zig-Zag pattern in strings in JavaScript?
We need to write a function that reads a string and converts the odd indexed characters in the string to upperCase and the even ones to lowerCase and returns a new string. Understanding the Pattern A zig-zag pattern alternates between lowercase and uppercase characters based on their index position. Even indices (0, 2, 4...) become lowercase, while odd indices (1, 3, 5...) become uppercase. Example Implementation const text = 'Hello world, it is so nice to be alive.'; const changeCase = (str) => { const newStr = str ...
Read MoreRemoving duplicates and inserting empty strings in JavaScript
We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end. Problem Description If we find 4 duplicate values we have to remove them all and insert four empty strings at the end. This maintains the original array length while eliminating duplicates. How It Works The algorithm uses reduce() to iterate through the array. For each element, it checks if the current index matches the last occurrence using lastIndexOf(). If yes, it's the final occurrence and gets added to the ...
Read MoreLength of longest string chain in JavaScript
A word chain problem involves finding sequences of words where each word can be formed by adding exactly one character to the previous word. This is a classic dynamic programming problem that requires checking predecessor relationships and building chains efficiently. Understanding Word Chains A word1 is a predecessor of word2 if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac" because we can insert 'a' at position 2. A word chain is a sequence of words [word_1, word_2, ..., word_k] where each word ...
Read MoreExpanding binomial expression using JavaScript
We need to write a JavaScript function that expands binomial expressions of the form (ax+b)^n, where a and b are integers, x is a variable, and n is a natural number. The function returns the expanded polynomial as a string. Problem Statement Given an expression like (8a+6)^4, we need to expand it using the binomial theorem. The result should be in the form ax^b+cx^d+ex^f... with coefficients and powers in decreasing order. Understanding the Binomial Theorem The binomial theorem states that (x+y)^n = Σ(C(n, k) * x^(n-k) * y^k) where C(n, k) is the binomial coefficient. ...
Read MoreJavaScript Reverse the order of the bits in a given integer
We are required to write a JavaScript program that reverses the order of the bits in a given integer. For example − 56 -> 111000 after reverse 7 -> 111 Another example, 234 -> 11101010 after reverse 87 -> 1010111 How It Works The algorithm converts the number to binary, reverses the binary string, and converts back to decimal: Convert number to binary string using toString(2) Split into array, reverse, and join back Parse reversed binary string to decimal using parseInt(reversedBinary, 2) Example const ...
Read MoreGet the max n values from an array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n, as the second argument. Our function should then pick the n greatest numbers from the array and return a new array consisting of those numbers. Using Sort and Slice The most straightforward approach is to sort the array in descending order and take the first n elements: const arr = [3, 4, 12, 1, 0, 5, 22, 20, 18, 30, 52]; const pickGreatest = (arr = [], num = 1) ...
Read MoreFinding the longest Substring with At Least n Repeating Characters in JavaScript
We are required to write a JavaScript function that takes in a string as the first argument and a positive integer n as the second argument. The string is likely to contain some repeating characters. The function should find out and return the length of the longest substring from the original string in which all characters appear at least n number of times. For example, if the input string is 'kdkddj' and n is 2, the output should be 5 because the desired longest substring is 'kdkdd'. Problem Understanding Given a string and a number n, ...
Read MoreLooping numbers with object values and push output to an array - JavaScript?
In JavaScript, you can create an array from an object by mapping object values to specific positions using Array.from(). This technique is useful when you have sparse data that needs to be converted into a dense array format. Problem Overview Given an object with numeric keys and values, we want to create an array where the object values are placed at positions corresponding to their keys, filling missing positions with a default value. var numberObject = { 2: 90, 6: 98 }; console.log("The original object:"); console.log(numberObject); The original object: { '2': 90, '6': ...
Read MoreFetching object keys using recursion in JavaScript
When working with nested objects in JavaScript, you often need to search for specific keys at any level of nesting. Recursion provides an elegant solution for traversing these complex data structures. The Problem Consider a nested object structure where we need to find all values for a specific key across all levels: const people = { Ram: { fullName: 'Ram Kumar', details: { age: ...
Read MoreConstructing a nested JSON object in JavaScript
In JavaScript, we often need to construct nested objects from structured data. This article demonstrates how to build a nested JSON object from a string containing paired characters. Problem Statement Given a string with characters in pairs, we need to construct a nested object where each pair becomes a code property, and each level has a sub-object for the next pair. Input string: const str = "AABBCCDDEE"; Expected output structure: const obj = { code: "AA", sub: { ...
Read More