Front End Technology Articles

Page 180 of 652

Sort an array to have specific items first in the array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

When working with arrays of objects, you might need to sort them so that items with specific properties appear first. This is a common requirement in applications where you need to prioritize certain elements while maintaining the original order of others. Suppose we have an array of objects like this: const arr = [ {flag: true, other: 1}, {flag: true, other: 2}, {flag: false, other: 3}, {flag: true, other: 4}, {flag: true, other: 5}, ...

Read More

Counting the number of palindromes that can be constructed from a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 421 Views

We are required to write a JavaScript function that takes in a string of characters as the first argument, say str, and a number, say num, as the second argument. The function should count the number of palindrome strings all exactly of length num can be constructed from the given string str. The function should then finally return the count. Problem Example If the input string and the number is: const str = 'ij'; const num = 4; Then the output should be: 4 because those four possible palindrome ...

Read More

Merge two objects in JavaScript ignoring undefined values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

When merging two objects in JavaScript, you may want to ignore undefined values to avoid overwriting valid data. The spread operator alone doesn't handle this logic, so we need a custom approach. The Problem Consider these two objects where some properties have undefined values: const A = { activity: 'purchased', count: undefined, time: '09:05:33' }; const B = { activity: 'purchased', count: '51', time: undefined }; // Simple spread operator overwrites values const simpleSpread = { ...A, ...B }; console.log("Simple spread:", simpleSpread); Simple spread: { activity: 'purchased', count: '51', time: undefined } ...

Read More

Counting How Many Numbers Are Smaller Than the Current Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 340 Views

In JavaScript, we often need to count how many elements in an array are smaller than each current element. This creates a new array where each position contains the count of smaller elements from the original array. Problem Statement Given an array of numbers, create a new array where each element represents the count of numbers smaller than the corresponding element in the original array. For example, if the input array is: [2, 7, 3, 1, 56, 4, 7, 8] The output should be: [1, 4, 2, 0, 7, 3, 4, ...

Read More

Convert array into array of subarrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

We are required to write a JavaScript function that takes in an array of literals and returns a new array that have elements of the original array chunked into subarrays of length exactly 2. Now if the length of original array is not exactly divisible by 2, then the last subarray should have only one element. For example, if the input array is: const arr = [1, 2, 3, 4, 5, 6, 7]; Then the output should be: const output = [[1, 2], [3, 4], [5, 6], [7]] Using Loop-based ...

Read More

Finding two numbers that produce equal to the sum of rest in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 374 Views

We have a sequence of numbers from 1 to any arbitrary number. We need to find pairs of numbers (m and n) from this sequence such that the sum of all remaining numbers equals the product of these two numbers. Problem Statement Given a number num, find all pairs [m, n] where: sum(1 to num) - (m + n) = m * n For example, if num = 10: Sum of 1 to 10 = 55 For pair [6, 7]: 55 - (6 + 7) = 42 and 6 × 7 = 42 ...

Read More

JavaScript Algorithm - Removing Negatives from the Array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 367 Views

Given an array X of multiple values (e.g. [-3, 5, 1, 3, 2, 10]), we need to write a function that removes any negative values from the array using only the pop() method without creating temporary arrays. The challenge is to modify the original array in-place, maintaining only positive numbers while using just the pop() method for removal. Algorithm Approach The algorithm works in two phases: Remove all negative values from the end of the array For remaining negatives, replace them with the last positive element and pop Complete Implementation function ...

Read More

Number of digits that divide the complete number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 215 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should count and return the number of digits present in the number that completely divide the number. For example, if the input number is 148, the output should be 2 because 148 is exactly divisible by 1 and 4 but not 8. Example Input and Output Input: 148 Output: 2 This is because: 148 ÷ 1 = 148 (divisible) 148 ÷ 4 = 37 (divisible) ...

Read More

How to join JavaScript array of string

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 199 Views

We need to write a JavaScript function that joins an array of strings, replaces all whitespaces with dashes "-", and returns the formatted string. For example: If the array is − const arr = ["QA testing promotion ", " Twitter ", "Facebook ", "Test"]; Then the output should be − const output = "QA-testing-promotion-Twitter-Facebook-Test"; Method 1: Using Loop and String Concatenation This approach joins the array elements and processes each character to replace spaces with dashes: const arr = ["QA testing promotion ", " Twitter ", "Facebook ", ...

Read More

Determining a pangram string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

A pangram is a string that contains every letter of the English alphabet at least once. Examples include "The quick brown fox jumps over the lazy dog" and "Pack my box with five dozen liquor jugs". We need to write a JavaScript function that determines whether a given string is a pangram. For this problem, we'll focus on the 26 letters of the English alphabet, ignoring case sensitivity. How It Works The algorithm creates an array of all 26 letters, then iterates through the input string. For each letter found in the string, it removes that letter ...

Read More
Showing 1791–1800 of 6,519 articles
« Prev 1 178 179 180 181 182 652 Next »
Advertisements