Object Oriented Programming Articles

Page 19 of 589

Comparing adjacent element and swap - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 457 Views

Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if they are in the wrong order. This process repeats until the array is sorted. How Bubble Sort Works The algorithm repeatedly steps through the array, compares adjacent elements, and swaps them if they are in the wrong order. Each pass "bubbles" the largest element to its correct position. Bubble Sort Process Initial: 10 30 5 ...

Read More

Checking a Checkbox with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 608 Views

In JavaScript, you can programmatically check a checkbox by setting its checked property to true. This is useful for form validation, user interactions, or initializing form states. HTML Structure First, let's look at a basic checkbox structure: John David Using the checked Property The checked property is a boolean that determines whether a checkbox is selected. Set it to true to check the checkbox, or false to uncheck it. Checkbox Example ...

Read More

Compare two arrays of single characters and return the difference? JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 535 Views

We are required to compare, and get the difference, between two arrays containing single character strings appearing multiple times in each array. Example of two such arrays are: const arr1 = ['A', 'C', 'A', 'D']; const arr2 = ['F', 'A', 'T', 'T']; We will check each character at the same position and return only the parts who are different. Approach The algorithm compares elements at the same index position. When characters differ, both are added to the result array. Any remaining elements from the longer array are also included. Example ...

Read More

Difference between two times using Dayjs JavaScript library?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 550 Views

Day.js is a lightweight JavaScript library for date manipulation. The diff() method calculates the difference between two time instances and returns the result in the specified unit. Syntax dayjs().diff(date, unit) Parameters date - The date to compare with unit - The unit of measurement: 'milliseconds', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years' Example: Basic Time Difference Day.js Time Difference ...

Read More

Finding trailing zeros of a factorial JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 466 Views

Given an integer n, we have to write a function that returns the number of trailing zeroes in n! (factorial). For example: trailingZeroes(4) = 0 trailingZeroes(5) = 1 because 5! = 120 trailingZeroes(6) = 1 How It Works Trailing zeros are created by multiplying factors of 10, which come from pairs of 2 and 5. Since there are always more factors of 2 than 5 in any factorial, we only need to count factors of 5. We count multiples of 5, then 25 (5²), then 125 (5³), and so on. Example const num = 17; const findTrailingZeroes = num => { let cur = 5, total = 0; while (cur

Read More

What is the best way to reduce and merge a collection of objects – JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 703 Views

The best way to reduce and merge a collection of objects is to use Object.values() combined with reduce() to group objects by a key and merge their properties. This approach is useful when you have duplicate objects and want to consolidate them, such as combining student records with the same ID. Example Data Consider this collection of student objects with duplicates: var details = [ { studentId: 10, marks: 75, studentName: "John" }, { studentId: 10, marks: 75, studentName: "John" }, { studentId: ...

Read More

Determining isomorphic strings JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 843 Views

Two strings are isomorphic if characters in one string can be mapped to characters in another string while preserving the structure. Each character must map to exactly one character, and no two characters can map to the same character. Understanding Isomorphic Strings For strings to be isomorphic: They must have the same length Characters at the same positions must follow a consistent mapping pattern The mapping must be bijective (one-to-one) Example const str1 = 'egg'; const str2 = 'add'; // Check if strings are isomorphic const isIsomorphic = (str1 = '', str2 ...

Read More

How to convert a string with zeros to number in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 446 Views

When you have a string containing numbers with dots or zeros as separators, you can convert it to a number using JavaScript's built-in methods. This is commonly needed when processing formatted numeric data. The Problem Consider a string like "453.000.00.00.0000" where dots are used as thousand separators rather than decimal points. Direct conversion methods like Number() won't work correctly because JavaScript interprets the first dot as a decimal separator. let stringValue = "453.000.00.00.0000"; console.log("Original string:", stringValue); console.log("Direct Number() conversion:", Number(stringValue)); // NaN Original string: 453.000.00.00.0000 Direct Number() conversion: NaN Using parseInt() ...

Read More

Removing duplicates and keep one instance in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 285 Views

In JavaScript, you often need to remove duplicate values from an array while keeping only one instance of each value. There are several effective methods to accomplish this task. Method 1: Using Set (Recommended) The most modern and efficient approach uses the Set object, which automatically removes duplicates: const arr = [1, 5, 7, 4, 1, 4, 4, 6, 4, 5, 8, 8]; // Using Set to remove duplicates const uniqueArr = [...new Set(arr)]; console.log(uniqueArr); [ 1, 5, 7, 4, 6, 8 ] Method 2: Using filter with indexOf ...

Read More

How to make filter and word replacement method - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 326 Views

In JavaScript, there's no built-in method to replace all occurrences of a word in a string. You can create custom functions using methods like split() and join(), or use modern approaches like replaceAll(). Let's explore different methods to filter and replace words in a string: var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript"; Method 1: Using split() and join() This method splits the string by the target word and joins the parts with the replacement: var sentence = "Yes, My Name ...

Read More
Showing 181–190 of 5,881 articles
« Prev 1 17 18 19 20 21 589 Next »
Advertisements