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
Object Oriented Programming Articles
Page 161 of 589
Counting the number of redundant characters in a string - JavaScript
We are required to write a JavaScript function that takes in a string and returns the count of redundant characters in the string. A redundant character is any character that appears more than once, where all duplicate occurrences (except the last one) are considered redundant. For example − If the string is − const str = 'abcde' Then the output should be 0 because each character appears only once. If the string is − const str = 'aaacbfsc'; Then the output should be 3 because 'a' appears 3 times (2 ...
Read MoreSleep in JavaScript delay between actions?
To create a sleep or delay in JavaScript, use setTimeout() for simple delays or async/await with Promises for more control. JavaScript doesn't have a built-in sleep function like other languages. 1000 milliseconds = 1 second 2000 milliseconds = 2 seconds, etc. Using setTimeout() for Delays setTimeout() executes a function after a specified delay. Here's an example with a 3-second delay: console.log("Starting calculation..."); setTimeout(function() { var firstValue = 10; var secondValue = 20; var result = firstValue + secondValue; ...
Read MoreSorting digits of all the number of array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and reorders the digits of all the numbers internally in a specific order (let's say in ascending order for the sake of this problem). For example − If the array is − const arr = [543, 65, 343, 75, 567, 878, 87]; Then the output should be − const output = [345, 56, 334, 57, 567, 788, 78]; Approach The solution involves converting each number to a string, splitting it into individual digits, sorting those ...
Read MoreExtract key value from a nested object in JavaScript?
Extracting keys from nested objects is a common requirement in JavaScript. This tutorial shows how to find the parent object and nested property that contains a specific value. Creating a Nested Object Let's start with a nested object containing teacher and subject information: var details = { "teacherDetails": { "teacherName": ["John", "David"] }, "subjectDetails": { "subjectName": ["MongoDB", "Java"] } } console.log("Nested object created:", ...
Read MoreJoining two strings with two words at a time - JavaScript
We are required to write a JavaScript function that takes in two strings, creates and returns a new string with first two characters of first string, next two characters of second string, then first, then second and so on. For example: If the strings are: const str1 = 'Hello world'; const str2 = 'How are you btw'; Then the output should be: 'HeHollw o arwoe rlyodu btw' Approach The algorithm alternates between the two strings, taking two characters at a time. When one string is exhausted, it appends the ...
Read MoreHow to disallow altering of object variables in JavaScript?
JavaScript provides Object.freeze() to make objects immutable, preventing addition, deletion, or modification of properties. This is useful when you need to protect object data from accidental changes. Syntax Object.freeze(object) Basic Example When an object is frozen, attempts to modify its properties are silently ignored: const canNotChangeTheFieldValueAfterFreeze = { value1: 10, value2: 20 }; Object.freeze(canNotChangeTheFieldValueAfterFreeze); // Attempt to change property (will be ignored) canNotChangeTheFieldValueAfterFreeze.value1 = 100; console.log("After changing the field value1 from 10 to 100 = " + canNotChangeTheFieldValueAfterFreeze.value1); ...
Read MoreConverting a proper fraction to mixed fraction - JavaScript
A proper fraction is one where the numerator is smaller than the denominator, represented in p/q form where both p and q are natural numbers. What is a Mixed Fraction? When we divide the numerator (a) of a fraction by its denominator (b), we get a quotient (q) and remainder (r). The mixed fraction form for fraction a/b is: Mixed form: q r b This is pronounced as "q wholes and r by b". Problem Statement We need to write a ...
Read MoreHow to add properties from one object into another without overwriting in JavaScript?
When merging objects in JavaScript, you often want to add properties from one object to another without overwriting existing values. This preserves the original object's data while incorporating new properties. The Problem Consider these two objects where some properties overlap: var first = {key1: 100, key2: 40, key3: 70}; var second = {key2: 80, key3: 70, key4: 1000}; console.log("First object:", first); console.log("Second object:", second); First object: { key1: 100, key2: 40, key3: 70 } Second object: { key2: 80, key3: 70, key4: 1000 } We want to add key4 from ...
Read MoreImplementing Priority Sort in JavaScript
We are required to write a JavaScript function that takes in two arrays of numbers, second being smaller in size than the first. Our function should return a sorted version of the first array (in increasing order) but put all the elements that are common in both arrays to the front. For example − If the two arrays are − const arr1 = [5, 4, 3, 2, 1]; const arr2 = [2, 3]; Then the output should be − [2, 3, 1, 4, 5] How It Works The priority ...
Read MoreUpdate array of objects with JavaScript?
In JavaScript, you can update an array of objects by modifying existing objects or adding new ones. This is commonly done using methods like push(), splice(), or array methods like map() and find(). Let's say we have the following array of objects: var studentDetails = [ { firstName: "John", listOfSubject: ['MySQL', 'MongoDB']}, {firstName: "David", listOfSubject: ['Java', 'C']} ]; console.log("Initial array:", studentDetails); Initial array: [ { firstName: 'John', listOfSubject: [ 'MySQL', 'MongoDB' ] }, { firstName: 'David', listOfSubject: [ 'Java', 'C' ] } ] ...
Read More