Articles on Trending Technologies

Technical articles with clear explanations and examples

Set the value in local storage and fetch – JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 653 Views

JavaScript's localStorage provides a simple way to store data persistently in the user's browser. Use localStorage.setItem() to store values and localStorage.getItem() to retrieve them. Syntax // Set a value localStorage.setItem("keyName", "value"); // Get a value let value = localStorage.getItem("keyName"); // Remove a value localStorage.removeItem("keyName"); // Clear all localStorage data localStorage.clear(); Setting Values in localStorage The setItem() method accepts two parameters: a key name and the value to store. Values are always stored as strings. ...

Read More

Deep Search JSON Object JavaScript

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

Deep searching a JSON object means recursively traversing through all nested objects and arrays to find elements that match specific criteria. This is useful when you need to locate objects buried deep within complex data structures. The Problem Suppose we have the following nested JSON object: const obj = { id: 1, title: 'hello world', child: { id: null, title: 'foobar', ...

Read More

Construct an identity matrix of order n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 970 Views

An identity matrix is a square matrix where all diagonal elements are 1 and all other elements are 0. This type of matrix is fundamental in linear algebra and has the property that when multiplied with any matrix, it returns the original matrix unchanged. What is an Identity Matrix? An identity matrix of order n is an n × n square matrix where: All diagonal elements (where row index equals column index) are 1 All other elements are 0 For example, an identity matrix of order 3 will be: [ [1, ...

Read More

Kit-Kat array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 210 Views

In JavaScript, a Kit-Kat array is a custom array where numbers are replaced with specific strings based on divisibility rules. This pattern is similar to the classic FizzBuzz problem but with customizable divisors. Problem Statement Create a function that takes three parameters: a natural number num, and two divisors m and n. The function should return an array containing numbers from 1 to num with these replacements: Replace multiples of m with 'kit' Replace multiples of n with 'kat' Replace multiples of both m ...

Read More

Finding maximum number from two arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 717 Views

We need to create a JavaScript function that takes two arrays of single-digit numbers and returns a new array representing the maximum possible number of a specified length. The function must preserve the relative order of elements from each original array. Problem Statement Given two arrays representing numbers and a target length num, we need to: Select digits from both arrays to form the largest possible number Maintain the relative order within each array Return exactly num digits Example Input and Output const arr1 = [1, 3, 4, 5, 6]; const arr2 ...

Read More

Third smallest number in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 639 Views

Problem We are required to write a JavaScript function that takes in an array of numbers of length at least three. Our function should simply return the third smallest number from the array. Example Following is the code − const arr = [6, 7, 3, 8, 2, 9, 4, 5]; const thirdSmallest = () => { const copy = arr.slice(); for(let i = 0; i < 2; i++){ const minIndex = copy.indexOf(Math.min(...copy)); copy.splice(minIndex, ...

Read More

Changing second half of string number digits to zero using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 242 Views

Problem We are required to write a JavaScript function that takes in a string number as the only argument. Our function should return the input number with the second half of digits changed to 0. In cases where the number has an odd number of digits, the middle digit onwards should be changed to 0. For example: 938473 → 938000 How It Works The algorithm works by calculating the midpoint of the string. For even-length strings, exactly half the digits are preserved. For odd-length strings, the middle digit becomes part of ...

Read More

Convert number to a reversed array of digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 235 Views

Given a non-negative integer, we need to write a function that returns an array containing the digits in reverse order. This is a common programming challenge that demonstrates string manipulation and array operations. Example 348597 => The correct solution should be [7, 9, 5, 8, 4, 3] Method 1: Using String Split and Reverse The most straightforward approach is to convert the number to a string, split it into individual characters, reverse the array, and convert back to numbers: const num = 348597; const reverseArrify = num => { ...

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

How to automate this object using JavaScript to set one key on each iteration as null?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 203 Views

When working with JavaScript objects, you might need to systematically set each property to null one at a time. This can be useful for testing, validation, or creating variations of an object. The most efficient approach is using Object.keys() with the spread operator. Understanding the Problem Given an object with multiple properties, we want to create new objects where exactly one property is set to null in each iteration, while keeping all other properties unchanged. Using Object.keys() with Spread Operator The Object.keys() method returns an array of property names, which we can iterate through. The spread ...

Read More
Showing 16261–16270 of 61,297 articles
Advertisements