Javascript Articles

Page 105 of 534

Check if the elements of the array can be rearranged to form a sequence of numbers or not in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 178 Views

We are required to write a JavaScript function that takes in an array of numbers and checks if the elements of the array can be rearranged to form a sequence of numbers or not. For example: If the array is − const arr = [3, 1, 4, 2, 5]; Then the output should be true because these numbers can be rearranged to form the consecutive sequence: 1, 2, 3, 4, 5. Approach To solve this problem, we need to: Sort the array in ascending order Check if each element is exactly ...

Read More

Finding the continuity of two arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 199 Views

We are required to write a JavaScript function that takes in two arrays of numbers. The function should return true if the two arrays upon combining can form a consecutive sequence, false otherwise. For example: If the arrays are − const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7]; When combined and sorted, these arrays form [1, 2, 3, 4, 5, 6, 7, 8, 9], which is a consecutive sequence. Therefore, the output should be true. Understanding Consecutive Sequences A consecutive sequence means each number is exactly ...

Read More

Performing the subtraction operation without the subtraction operator in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 182 Views

We are required to write a JavaScript function that takes in two numbers and returns their difference but without using the (-) sign. This problem can be solved using bitwise operations. The key insight is that subtraction can be performed using XOR (^) and AND (&) operations combined with bit shifting. How It Works The algorithm uses these bitwise operations: XOR (^): Performs subtraction without handling borrows AND (&): Identifies positions where borrowing is needed Left shift ( { console.log(`Step ${step}: a=${a}, b=${b}`); if(b === 0){ console.log(`Result: ${a}`); return a; } const xor = a ^ b; const borrow = (~a & b)

Read More

Dynamic programming to check dynamic behavior of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 598 Views

We are required to write a JavaScript function that takes in an array of strings, ordered by ascending length. The function should return true if, for each pair of consecutive strings, the second string can be formed from the first by adding a single letter either at the beginning or end. For example: If the array is given by − const arr = ["c", "ca", "can", "acan", "acane", "dacane"]; Then our function should return true because: "ca" = "c" + "a" (adding "a" at the end) ...

Read More

Grouping of same kind of numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 210 Views

We have an array of numbers with both negative and non-negative values. Our goal is to count consecutive groups of non-negative numbers (positives and zeros) in the array. const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; console.log("Array:", arr); Array: [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0] In this array, we need to identify consecutive groups of non-negative numbers. Looking at the array: Index 3: single element 0 forms one group Indices 9-11: elements 0, 1, 0 form another group ...

Read More

Similarities between different strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 188 Views

In JavaScript, finding similarities (intersections) between two arrays of strings is a common operation. We need to write a function that computes the intersection of two string arrays and returns elements that appear in both arrays. Problem Statement Given two arrays of strings, find the common elements that exist in both arrays. Each element in the result should appear as many times as it shows in both arrays. Example If we have these input arrays: arr1 = ['hello', 'world', 'how', 'are', 'you']; arr2 = ['hey', 'world', 'can', 'you', 'rotate']; The expected output ...

Read More

Splitting an array into chunks in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 3K+ Views

In JavaScript, splitting an array into smaller chunks of equal size is a common operation. This article covers multiple approaches to divide array elements into n-sized chunks effectively. Input and Output Example: Consider an array with elements [10, 20, 30, 40] and chunk size of 2: Input = [10, 20, 30, 40] Output = [[10, 20], [30, 40]] The array gets divided into chunks where each sub-array contains the specified number of elements. Using slice() Method with for Loop The slice() method creates a shallow copy of a portion of an array ...

Read More

Finding the mid of an array in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 3K+ Views

Finding the middle element of an array is a common programming task in JavaScript. The approach differs depending on whether the array has an odd or even number of elements. Array Basics An Array in JavaScript is a data structure used to store multiple elements. These elements are stored at contiguous memory locations and accessed using index numbers starting from 0. Syntax const array_name = [item1, item2, ...]; Example of array declaration: const body = ['Eyes', 'Nose', 'Lips', 'Ears']; Understanding Middle Element Logic Finding the middle element depends ...

Read More

Upper or lower elements count in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 149 Views

When working with arrays of numbers, you may need to count how many elements are above or below a specific threshold value. This is useful for data analysis, filtering, and statistical operations. Consider we have an array of numbers that looks like this: const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78]; We need to write a function that counts how many elements in the array are below and above a given number. For example, if the threshold number is 60: Elements below ...

Read More

Listing all the prime numbers upto a specific number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 240 Views

We are required to write a JavaScript function that takes in a number, say n, and returns an array containing all the prime numbers up to n. For example: If the number n is 24. Then the output should be − const output = [2, 3, 5, 7, 11, 13, 17, 19, 23]; Therefore, let's write the code for this function − Method 1: Using Helper Function for Prime Check This approach uses a helper function to check if a number is prime, then iterates through numbers up to n: ...

Read More
Showing 1041–1050 of 5,340 articles
« Prev 1 103 104 105 106 107 534 Next »
Advertisements