AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 435 of 840

Get the property of the difference between two objects in JavaScript

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

When working with JavaScript objects, you often need to compare them and identify which properties have different values. This is useful for tracking changes, validation, or debugging purposes. Problem Overview Given two objects with similar key-value pairs, we need to write a function that finds the first key with different values between the objects. If all values match, the function should return -1. Here are sample objects to demonstrate the concept: const obj1 = { name: 'Rahul Sharma', id: '12342fe4554ggf', isEmployed: true, ...

Read More

Recursively loop through an array and return number of items with JavaScript?

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

We need to write a function that recursively searches through a nested array and counts how many times a specific value appears. This is useful when working with complex nested data structures. Problem Statement Given a nested array, we want to count all occurrences of a search term, including items in nested sub-arrays. const names = ["rakesh", ["kalicharan", "krishna", "rakesh", "james", ["michael", "nathan", "rakesh", "george"]]]; For the above array, searching for "rakesh" should return 3 because it appears once at the top level and twice in nested arrays. Recursive Solution Here's a ...

Read More

Highlight a text, every time page loads with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 357 Views

To highlight text every time a page loads in JavaScript, you can use a for loop to iterate through words and wrap specific words in elements with CSS classes for styling. Approach The technique involves: Getting the text content using getElementById() Splitting the text into an array of words using split(" ") Looping through each word to find matches Wrapping matching words with tags Rejoining the array and updating the HTML content Example Text ...

Read More

Share methods in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 708 Views

Methods can be shared across multiple object instances by attaching them to the prototype property. This approach ensures all instances of a constructor function share the same method, making memory usage more efficient than defining methods inside the constructor. Basic Prototype Method Sharing When you define a method on the prototype, all instances created from that constructor function can access it: Shared Methods Shared Methods in JavaScript ...

Read More

Convert number to alphabet letter JavaScript

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

We are required to write a function that takes in a number between 1 and 26 (both inclusive) and returns the corresponding English alphabet for it. (capital case) If the number is out of this range return -1. For example: toAlpha(3) = C toAlpha(18) = R The ASCII Codes ASCII codes are the standard numerical representation of all the characters and numbers present on our keyboard and many more. The capital English alphabets are also mapped in the ASCII char codes, they start from 65 and go all the way up to 90, ...

Read More

Sum of consecutive numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 574 Views

Let's say, we have to write a function that takes in an array and returns another array in which the consecutive similar numbers are added up together. For example − const array = [1, 5, 5, 5, 8, 8, 9, 1, 4, 4, 2]; console.log("Original array:", array); Original array: [ 1, 5, 5, 5, 8, 8, 9, 1, 4, 4, 2 ] The output should be − [1, 15, 16, 9, 1, 8, 2] All consecutive 5s added up to 15, then 2 consecutive 8s added up to ...

Read More

Static Properties in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 190 Views

Static properties in JavaScript belong to the class itself rather than its instances. They can be accessed directly on the class without creating objects, making them useful for storing class-level data like constants or shared values. Syntax // Function constructor approach function ClassName(params) { // instance properties } ClassName.staticProperty = value; // ES6 class approach class ClassName { static staticProperty = value; } Example: Static Properties with Function Constructor ...

Read More

Completely removing duplicate items from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 360 Views

We are required to write a function that takes in an array and returns a new array that have all duplicate values removed from it. The values that appeared more than once in the original array should not even appear for once in the new array. For example, if the input is: const arr = [23, 545, 43, 232, 32, 43, 23, 43]; The output should be: [545, 232, 32] Understanding indexOf() vs lastIndexOf() Array.prototype.indexOf() → Returns the index of first occurrence of searched ...

Read More

Get the longest and shortest string in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 792 Views

We have an array of string literals like this: const arr = ['Some', 'random', 'words', 'that', 'actually', 'form', 'a', 'sentence.']; We need to write a function that returns the longest and shortest word from this array. We will use the Array.prototype.reduce() method to keep track of the longest and shortest word during iteration. Using Array.reduce() Method The reduce() method processes each element and maintains an accumulator object containing both the longest and shortest strings found so far: const arr = ['Some', 'random', 'words', 'that', 'actually', 'form', 'a', 'sentence.']; const findWords = ...

Read More

How to check if an object is an instance of a Class in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 470 Views

In JavaScript, you can check if an object is an instance of a specific class using the instanceof operator. This operator returns true if the object was created by the specified constructor function or class. Syntax object instanceof Constructor Using instanceof with Constructor Functions Instance Check Example CHECK INSTANCE function Student(name, age, standard) { this.name = name; this.age = age; this.standard = standard; } let student1 = new Student("Rohan", ...

Read More
Showing 4341–4350 of 8,392 articles
« Prev 1 433 434 435 436 437 840 Next »
Advertisements