AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 281 of 840

Find all substrings combinations within arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 452 Views

We are required to write a JavaScript function that takes in an array of strings. The function should find all the substring and superstring combinations that exist in the array and return an array of those elements. For example − If the array is − const arr = ["abc", "abcd", "abcde", "xyz"]; Then the output should be − const output = ["abc", "abcd", "abcde"]; because the first two are the substring of the last one. How It Works The algorithm compares each string with every other string in the ...

Read More

Ways to get to a specific sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 190 Views

Problem We are required to write a JavaScript function that takes in an array of integers, arr, as the first argument and a single integer, target, as the second argument. For each integer in the array, our function can either assign '+' or '-' to it. Our function should find out how many ways in total exist to assign '+', '-' to make the sum of integers of the array equal to the target sum. For example, if the input to the function is: const arr = [1, 1, 1, 1, 1]; const target = ...

Read More

Return a sorted array in lexicographical order in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 447 Views

We are required to write a JavaScript function that takes two arrays, say arr1 and arr2. Our function should return a sorted array in lexicographical order of the strings of arr1 which are substrings of strings of arr2. How It Works The function iterates through arr1 and checks if each string is a substring of any string in arr2. If found, it adds the string to the result array and continues to the next string using a labeled continue statement. Example The code for this will be − const lexicographicalSort = (arr1 = [], ...

Read More

How to replace some preceding characters with a constant 4 asterisks and display the last 3 as well - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 950 Views

In JavaScript, you can mask sensitive data by replacing preceding characters with asterisks while keeping the last few characters visible. This is commonly used for phone numbers, credit cards, or account numbers. Problem Statement Given these input values: '6778922' '76633 56 1443' '8888 4532 3232 9999' We want to replace all preceding characters with 4 asterisks and display only the last 3 characters: **** 922 **** 443 **** 999 Using replace() with Regular Expression The replace() method with a regex pattern can capture the last 3 characters and replace ...

Read More

Finding degree of subarray in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 602 Views

The degree of an array is defined as the maximum frequency of any element in the array. Finding the shortest subarray with the same degree is a common programming problem that involves tracking element frequencies and their positions. const arr = [1, 2, 3, 3, 5, 6, 4, 3, 8, 3]; console.log("Array:", arr); console.log("Degree of array: 4 (element 3 appears 4 times)"); Array: [ 1, 2, 3, 3, 5, 6, 4, 3, 8, 3 ] Degree of array: 4 (element 3 appears 4 times) Our task is to find the length of the ...

Read More

Counting number of vowels in a string with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 759 Views

We are required to write a JavaScript function that takes in a string. The function should count the number of vowels present in the string. The function should prepare an object mapping the count of each vowel against them. Example The code for this will be − const str = 'this is an example string'; const vowelCount = (str = '') => { const splitString = str.split(''); const obj = {}; const vowels = "aeiou"; splitString.forEach((letter) => { ...

Read More

Consecutive ones with a twist in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 243 Views

We are required to write a JavaScript function that takes in a binary array (an array that consists of only 0 and 1), arr, as the only argument. Our function should find the maximum number of consecutive 1s in this array if we can flip at most one 0. Problem Statement Given a binary array, find the maximum number of consecutive 1s we can achieve by flipping at most one 0 to 1. For example, if the input array is: [1, 0, 1, 1, 0] Then the output should be: 4 ...

Read More

Checking a Checkbox with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 619 Views

In JavaScript, you can programmatically check a checkbox by setting its checked property to true. This is useful for form validation, user interactions, or initializing form states. HTML Structure First, let's look at a basic checkbox structure: John David Using the checked Property The checked property is a boolean that determines whether a checkbox is selected. Set it to true to check the checkbox, or false to uncheck it. Checkbox Example ...

Read More

Longest subarray which only contains strictly increasing numbers JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 339 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument. The function should then return the length of the longest continuous subarray from the array that only contains elements in a strictly increasing order. A strictly increasing sequence is the one in which any succeeding element is greater than all its preceding elements. Example const arr = [5, 7, 8, 12, 4, 56, 6, 54, 89]; const findLongest = (arr) => { if(arr.length == 0) { ...

Read More

Finding the smallest good base in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 197 Views

For an integer num, we call k (k >= 2) a good base of num, if all digits of num base k are 1. For instance: 13 base 3 is 111, hence 3 is a good base for num = 13 Problem We are required to write a JavaScript function that takes in string str that represents a number as the only argument. The function should return the string representation of the smallest possible number which is a good base for str. For example, if the input to the function is: const str = ...

Read More
Showing 2801–2810 of 8,392 articles
« Prev 1 279 280 281 282 283 840 Next »
Advertisements