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 321 of 840
Swapping even and odd index pairs internally in JavaScript
We are required to write a JavaScript function that takes in an array of literals as the first and the only argument. Our function should swap each consecutive even index with each other, and swap each consecutive odd indexes with each other. The function should do these swappings in place. For example, if the input array is: const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8]; Then the array should become: const output = [2, 3, 0, 1, 6, 7, 4, 5, 8]; Because 0 and 2 ...
Read MoreConstructing a string of alternating 1s and 0s of desired length using JavaScript
Problem We are required to write a JavaScript function that takes in a number n. Starting with '1' our function should construct a string of length n that contains '1' and '0' alternatingly. Example Following is the code − const num = 12; const buildString = (num = 1) => { let res = ''; for(let i = 0; i < num; i++){ if(i % 2 === 0){ ...
Read MoreCenter of each side of a polygon in JavaScript
In JavaScript, finding the center (midpoint) of each side of a polygon involves calculating the average coordinates between consecutive vertices. This is useful in computational geometry for polygon analysis and graphics applications. Problem Definition Given an array of coordinate pairs representing polygon vertices, we need to find the midpoint of each side. Each side connects two consecutive vertices, and the last vertex connects back to the first vertex to close the polygon. // Example polygon coordinates (longitude, latitude) const polygonVertices = [ [-73.9280684530257, 40.8099975343718], [-73.9282820374729, 40.8100875554645], ...
Read MoreFinding sum of alternative elements of the array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the only argument. The function should calculate and return the sum of alternative elements of the array. For example − If the input array is − const arr = [1, 2, 3, 4, 5, 6, 7]; Then the output should be − 1 + 3 + 5 + 7 = 16 Method 1: Using for Loop with Index Check This approach uses a for loop and checks if the index is even to ...
Read MoreCounting n digit Numbers with all unique digits in JavaScript
We need to write a JavaScript function that counts all n-digit numbers where every digit appears exactly once (all digits are unique). Problem Statement Given a number num, find how many numbers exist with exactly num digits where all digits are unique. For example: 1-digit numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 → Count = 10 2-digit numbers: 10, 12, 13, ..., 98 (excluding 11, 22, 33, etc.) → Count = 91 Understanding the Logic This is a combinatorics problem: 1-digit: All 10 digits (0-9) are valid 2-digit: ...
Read MoreIterate over an object and remove false property in JavaScript
In JavaScript, you may need to recursively iterate through an object and remove properties with falsy values. This is common when cleaning up configuration objects or filtering data structures. The Problem Consider an object with nested structures containing "enabled" properties. We want to remove any objects where enabled is false, and also clean up any empty parent objects left behind. const obj = { a: { someKey: { propOne: '', ...
Read MoreChecking for overlapping times JavaScript
Time interval overlap detection is a common programming challenge. We need to determine if any two time intervals in an array share common time periods. Problem Definition Given an array of time intervals with start and end times, we need to check if any two intervals overlap. Two intervals overlap when they have some time in common. const arr = [ { start: '01:00', end: '04:00' }, { start: '05:00', end: '08:00' }, { start: '07:00', end: '11:00' }, { start: '09:30', end: '18:00' ...
Read MoreChecking if one string can be achieved from another with single tweak in JavaScript
We are required to write a JavaScript function that takes in two strings of characters lets call them str1 and str2. The function should check whether we can form str2 from str1 by deleting exactly one character from str1. If we can do so the function should return true, false otherwise. For example − If the input strings are − const str1 = 'chemistty'; const str2 = 'chemisty'; Then the output should be − const output = true; Approach The solution involves checking if the length difference is ...
Read MoreRemoving least number of elements to convert array into increasing sequence using JavaScript
We need to write a JavaScript function that removes the minimum number of elements from an array to make it strictly increasing. This is essentially finding the Longest Increasing Subsequence (LIS) and removing all other elements. Problem Analysis The goal is to find the longest possible increasing subsequence and remove the fewest elements. A strictly increasing sequence means each element is greater than the previous one. Example with Corrected Algorithm The original approach has flaws - it doesn't find the optimal solution. Here's a correct implementation using dynamic programming: const arr = [1, 100, ...
Read MoreString replace multiple characters with an asterisk in JavaScript
We are required to write a JavaScript function that takes in a string as the first argument and an array of numbers. Our function should replace all the characters in the string at indices that are specified by the array elements taken as the second argument with an asterisk. Example The code for this will be − const str = "Lorem ipsum dolor sit amet consectetur adipiscing elit"; const arr = [4, 7, 9, 12, 15]; const replaceWithAsterisk = (str, indices) => { let res = ''; res ...
Read More