Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 435 of 840
Get the property of the difference between two objects in JavaScript
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 MoreRecursively loop through an array and return number of items with JavaScript?
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 MoreHighlight a text, every time page loads with JavaScript
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 MoreShare methods in JavaScript
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 MoreConvert number to alphabet letter JavaScript
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 MoreSum of consecutive numbers in JavaScript
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 MoreStatic Properties in JavaScript
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 MoreCompletely removing duplicate items from an array in JavaScript
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 MoreGet the longest and shortest string in an array JavaScript
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 MoreHow to check if an object is an instance of a Class in JavaScript?
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