AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 288 of 840

Absolute difference of Number arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 511 Views

In JavaScript, calculating the absolute difference between corresponding elements of two arrays is a common operation. This involves subtracting elements at the same index positions and taking the absolute value of the result. Suppose we have two arrays like these: const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [9, 8, 7, 5, 8, 3]; We need to create a function that returns an array of absolute differences between corresponding elements. For these arrays, the expected output should be: [8, 6, 4, 1, 3, 3] Using For Loop ...

Read More

Get intersection between two ranges in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 895 Views

In JavaScript, finding the intersection between two numerical ranges involves determining the overlapping portion. A range is typically represented as an array with two elements: [start, end]. Understanding Range Intersection The intersection of two ranges is the overlapping segment. For ranges [2, 5] and [4, 7], the intersection is [4, 5] because that's where they overlap. const arr1 = [2, 5]; const arr2 = [4, 7]; console.log("Range 1:", arr1); console.log("Range 2:", arr2); Range 1: [ 2, 5 ] Range 2: [ 4, 7 ] Algorithm To find the intersection, we ...

Read More

Comparing corresponding values of two arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 446 Views

When working with arrays in JavaScript, you might need to compare corresponding elements to determine which array has more "dominant" values at each position. This article demonstrates how to build a function that compares two arrays element by element and returns a result indicating which array has more greater values. Problem Statement Given two arrays of equal length, we need to compare corresponding elements and return: -1 if the first array has more elements greater than their corresponding elements in the second array 1 if the second array has more elements greater than their corresponding elements ...

Read More

Interchanging first letters of words in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 376 Views

We need to write a JavaScript function that takes a string containing exactly two words and swaps their first letters to create a new string. Problem Statement Given a string with two words separated by a space, we want to interchange the first character of each word while keeping the rest of the characters in their original positions. Example For input "hello world", we should get "wello horld" where 'h' and 'w' are swapped. const str = 'hello world'; const interchangeChars = (str = '') => { const [first, second] ...

Read More

How to get the most common values in array: JavaScript ?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 498 Views

We are required to write a JavaScript function that takes in an array of literals that have repeating values. Our function should return an array of the most common element(s) in the array (if two or more elements appear for the same number of most times then the array should contain all those elements). Example The code for this will be − const arr1 = ["a", "c", "a", "b", "d", "e", "f"]; const arr2 = ["a", "c", "a", "c", "d", "e", "f"]; const getMostCommon = arr => { const count = ...

Read More

Positive, negative and zeroes contribution of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 420 Views

In JavaScript, you may need to analyze an array of integers to find the fractional ratio of positive, negative, and zero values. This is useful for statistical analysis and data processing tasks. Problem Statement Given an array of integers containing positive, negative, and zero values: const arr = [23, -1, 0, 11, 18]; We need to write a function that calculates the fractional ratio for each group: negative, zero, and positive numbers. The output should be an array of three decimal values representing these ratios. For the above array with length 5, the ...

Read More

Number difference after interchanging their first digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 128 Views

We are required to write a JavaScript function that takes in an array of exactly two numbers. Our function should return the absolute difference between the numbers after interchanging their first digits. For instance, for the array [105, 413]: Original numbers: 105 and 413 After interchanging first digits: 405 and 113 The difference will be: |405 - 113| = 292 Algorithm The approach involves converting numbers to strings, extracting first digits, creating new numbers with swapped first digits, and calculating the absolute difference. Example const arr = [105, 413]; const ...

Read More

Return the index of first character that appears twice in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 343 Views

We are required to write a JavaScript function that takes in a string and returns the index of the first character that appears twice in the string. If there is no such character then we should return -1. Syntax function firstRepeating(str) { // Implementation here } Example The code for this will be − const str = 'Hello world, how are you'; const firstRepeating = str => { const map = new Map(); for(let i = 0; i < ...

Read More

Sort the second array according to the elements of the first array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 302 Views

Suppose, we have two arrays like these − const arr1 = ['d', 'a', 'b', 'c']; const arr2 = [{a:1}, {c:3}, {d:4}, {b:2}]; console.log("First array:", arr1); console.log("Second array:", arr2); First array: [ 'd', 'a', 'b', 'c' ] Second array: [ { a: 1 }, { c: 3 }, { d: 4 }, { b: 2 } ] We are required to write a JavaScript function that accepts these two arrays. The function should sort the second array according to the elements of the first array. We have to sort the keys of ...

Read More

Complete Equation by Filling Missing Operator in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 521 Views

We are required to write a JavaScript function that takes in a bunch of numbers and returns the correct sequence of operations to satisfy the equation. The operators that can be used are (+, −, *, /, ^, %). For example − Input : 5 3 8 Output : 5+3=8 Input : 9 27 3 Output : 9=27/3 Input : 5 2 25 , 1 5 2 Output : 5^2=25 , 1=5%2 For each input, there is at ...

Read More
Showing 2871–2880 of 8,392 articles
« Prev 1 286 287 288 289 290 840 Next »
Advertisements