Articles on Trending Technologies

Technical articles with clear explanations and examples

Implementing binary search in JavaScript to return the index if the searched number exist

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 714 Views

We are required to write a JavaScript function that takes in a sorted array of numbers as the first argument and a search number as the second argument. If the search number exists in the array, we need to return its index in the array, otherwise we need to return -1. We have to do this making use of the binary search algorithm. The binary search algorithm is basically a divide and conquer algorithm which recursively divides the array into halves until it converges to a singleton element. The sorting of array is necessary for binary search ...

Read More

Using a recursive function to capitalize each word in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 467 Views

We are required to write a JavaScript function that takes in an array of String literals. The function should do the following two things − Make use of recursive approach Make first word of each string element capital. Our function should do this without using extra space for storing another array. For example − If the input array is − const arr = ['apple', 'banana', 'orange', 'grapes']; Then the array should be transformed to − const output = ['Apple', 'Banana', 'Orange', 'Grapes']; ...

Read More

Replacing digits to form binary using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 600 Views

We need to write a JavaScript function that converts a string of digits into a binary-like representation by replacing digits below 5 with '0' and digits 5 and above with '1'. Problem Statement Given a string containing digits, transform it using these rules: Digits 0, 1, 2, 3, 4 → '0' Digits 5, 6, 7, 8, 9 → '1' Using For Loop const str = '262355677834342'; const convert = (str = '') => { let res = ''; for(let i = 0; i < ...

Read More

Merging and rectifying arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 192 Views

We need to create a JavaScript function that merges two arrays and removes duplicates, ensuring each element appears only once in the final result. Problem We are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and the second arguments. Our function should merge the elements of both these arrays into a new array and if upon merging or before merging there exists any duplicates, we should delete the excess duplicates so that only one copy of each element is present in the merged array. The ...

Read More

How to calculate GCD of two or more numbers/arrays in JavaScript?

Imran Alam
Imran Alam
Updated on 15-Mar-2026 850 Views

The greatest common divisor (GCD) of two or more numbers, also known as the greatest common factor (GCF) or highest common factor (HCF), is the largest positive integer that divides a given number without a remainder. In other words, the GCD is the largest number that is a divisor of both numbers. For example, the GCD of 24 and 36 is 12. Understanding the Euclidean Algorithm The Euclidean algorithm is the most efficient method to calculate GCD. It works by repeatedly applying the formula: gcd(a, b) = gcd(b, a % b) until one number becomes zero. ...

Read More

Top 7 Reasons to Love JavaScript

Rushi Javiya
Rushi Javiya
Updated on 15-Mar-2026 593 Views

JavaScript is a dynamic programming language for creating websites, web applications, video games, and many other interactive experiences. You can add dynamic features to websites that you couldn't achieve with only HTML and CSS. JavaScript is the programming language that powers modern web browsers to create dynamic, interactive content. You witness JavaScript in action whenever you see dropdown menus, content that loads without refreshing the page, or elements that change colors dynamically. Top 7 Reasons to Love JavaScript 1. Versatility Across Multiple Domains JavaScript serves multiple development needs effectively. While Python excels in machine learning and ...

Read More

How to Sort/Order keys in JavaScript objects ?

Rushi Javiya
Rushi Javiya
Updated on 15-Mar-2026 16K+ Views

In JavaScript, objects store key-value pairs, but their keys aren't automatically sorted. While we can't directly use the sort() method on objects like we do with arrays, we can sort the keys and create a new object with the sorted key-value pairs. Below, we will learn various methods to sort object keys in ascending and descending order. Using sort() Method to Order Keys We can use Object.keys() to get all object keys as an array, sort that array, then reconstruct the object with sorted keys. Syntax let allKeys = Object.keys(originalObject); allKeys.sort(); let sortedObj = ...

Read More

Add property to common items in array and array of objects - JavaScript?

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

To add properties to objects based on common items in arrays, use the map() method combined with Set for efficient lookups. This approach allows you to conditionally add properties to array objects based on whether their values exist in another array. Example Arrays Let's start with a simple array and an array of objects: const firstname = ['John', 'David', 'Bob']; const studentDetails = [ { firstname: 'Carol', marks: 78 }, ...

Read More

Merge and remove duplicates in JavaScript Array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 444 Views

Merging arrays and removing duplicates is a common task in JavaScript. There are several approaches to accomplish this, from traditional loops to modern ES6 methods. Problem Statement Given two arrays of numbers, we need to combine them into a single array where each element appears only once. const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6]; console.log("Array 1:", arr1); console.log("Array 2:", arr2); Array 1: [ 2, 4, 5, 3, 7, 8, 9 ] Array 2: [ 1, 4, 5, 2, 3, 7, ...

Read More

Finding smallest element in a sorted array which is rotated in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 188 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument. The array is first sorted and then rotated by any arbitrary number of elements. Our function should find the smallest element in the array and return that element. The only condition is that we have to do this in less than linear time complexity, using a modified binary search algorithm that achieves O(log n) time complexity. Problem Understanding In a rotated sorted array like [6, 8, 12, 25, 2, 4, 5], the original sorted array [2, 4, ...

Read More
Showing 16471–16480 of 61,297 articles
Advertisements