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 3 of 589
Variable defined with a reserved word const can be changed - JavaScript?
In JavaScript, variables declared with const cannot be reassigned, but this doesn't mean the contents of objects or arrays are immutable. You can still modify the properties of objects declared with const. Understanding const with Objects The const keyword prevents reassignment of the variable itself, but object properties can still be modified: const details = { firstName: 'David', subjectDetails: { subjectName: 'JavaScript' } }; // This works - modifying object properties details.firstName = 'John'; details.subjectDetails.subjectName = 'Python'; console.log(details); { firstName: 'John', subjectDetails: { ...
Read MoreHow do I turn a string in dot notation into a nested object with a value – JavaScript?
Converting a dot-notation string into a nested object is a common task in JavaScript. This approach uses split() and map() to dynamically build the nested structure. Problem Setup Let's say we have a dot-notation string representing nested property keys: const keys = "details1.details2.details3.details4.details5"; const firstName = "David"; We want to create a nested object where the final property gets the value "David". Solution Using split() and map() The technique involves splitting the dot-notation string and using map() to build nested objects: const keys = "details1.details2.details3.details4.details5"; const firstName = "David"; ...
Read MoreDisplay the result difference while using ceil() in JavaScript?
The Math.ceil() function rounds a number UP to the nearest integer. While division might give decimal results, Math.ceil() ensures you get the next whole number. How Math.ceil() Works If your value is 4.5, 4.3, or even 4.1, Math.ceil() will return 5. It always rounds UP, unlike regular rounding. Example: Division Without ceil() Let's see normal division first: var result1 = 98; var result2 = 5; console.log("The actual result is=" + result1 / result2); The actual result is=19.6 Example: Division With ceil() Now let's apply Math.ceil() to round ...
Read MoreHow do I make an array with unique elements (remove duplicates) - JavaScript?
In JavaScript, there are several ways to remove duplicate elements from an array and create a new array with unique values only. Using filter() with indexOf() The filter() method combined with indexOf() is a traditional approach that keeps only the first occurrence of each element: var duplicateNumbers = [10, 20, 100, 40, 20, 10, 100, 1000]; console.log("Original array:"); console.log(duplicateNumbers); var uniqueNumbers = duplicateNumbers.filter(function (value, index, array) { return array.indexOf(value) === index; }); console.log("Array with unique elements:"); console.log(uniqueNumbers); Original array: [ 10, 20, 100, ...
Read MoreHow to replace some preceding characters with a constant 4 asterisks and display the last 3 as well - JavaScript?
In JavaScript, you can mask sensitive data by replacing preceding characters with asterisks while keeping the last few characters visible. This is commonly used for phone numbers, credit cards, or account numbers. Problem Statement Given these input values: '6778922' '76633 56 1443' '8888 4532 3232 9999' We want to replace all preceding characters with 4 asterisks and display only the last 3 characters: **** 922 **** 443 **** 999 Using replace() with Regular Expression The replace() method with a regex pattern can capture the last 3 characters and replace ...
Read MoreSplitting a hyphen delimited string with negative numbers or range of numbers - JavaScript?
When working with hyphen-delimited strings containing negative numbers or ranges, simple split('-') won't work correctly because it treats every hyphen as a delimiter. We need regular expressions to distinguish between separating hyphens and negative number signs. The Problem Consider these strings with mixed content: var firstValue = "John-Smith-90-100-US"; var secondValue = "David-Miller--120-AUS"; Using split('-') would incorrectly split negative numbers and ranges, giving unwanted empty strings and broken values. Solution Using Regular Expression We use a lookahead pattern to split only at hyphens that precede letters or number ranges: var firstValue ...
Read MoreCompare the Triplets - JavaScript?
The "Compare the Triplets" problem is a popular algorithmic challenge where you compare two arrays of three integers each and count how many comparisons each array wins. Alice and Bob each have three scores, and we need to determine who wins more individual comparisons. Problem Statement Given two triplets (arrays of 3 elements each), compare corresponding elements and award points: 1 point to Alice if her element is greater, 1 point to Bob if his element is greater, 0 points for ties. Return an array with [Alice's score, Bob's score]. Example Input Alice: [5, 6, ...
Read MoreHow to redundantly remove duplicate elements within an array – JavaScript?
Let's say we have an array with duplicate elements like this: [10, 20, 10, 50, 60, 10, 20, 40, 50] JavaScript provides several methods to remove duplicate elements from arrays. The most common and efficient approach is using the Set object with the spread operator. Using Set with Spread Operator (Recommended) The Set object stores only unique values. Combined with the spread operator, it creates a new array without duplicates: var originalArray = [10, 20, 10, 50, 60, 10, 20, 40, 50]; var arrayWithNoDuplicates = [...new Set(originalArray)]; console.log("Original array:", originalArray); console.log("No ...
Read MoreHow to replace before first forward slash - JavaScript?
Let's say the following is our string with forward slash — var queryStringValue = "welcome/name/john/age/32" To replace before first forward slash, use replace() along with regular expressions. Syntax string.replace(/^[^/]+/, "replacement") Example Following is the code — var regularExpression = /^[^/]+/ var queryStringValue = "welcome/name/john/age/32" var replacedValue = queryStringValue.replace(regularExpression, 'index'); console.log("Original value=" + queryStringValue); console.log("After replacing the value=" + replacedValue); Original value=welcome/name/john/age/32 After replacing the value=index/name/john/age/32 How the Regular Expression Works The regular expression /^[^/]+/ breaks down as follows: ^ ...
Read MoreConvert number to a reversed array of digits in JavaScript
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