Object Oriented Programming Articles

Page 13 of 589

Replace multiple instances of text surrounded by specific characters in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 358 Views

Let's say we have a string where certain text is surrounded by special characters like hash (#). We need to replace these placeholders with actual values. var values = "My Name is #yourName# and I got #marks# in JavaScript subject"; We need to replace the special character placeholders with valid values. For this, we use replace() along with shift(). Using replace() with shift() The replace() method with a regular expression can find all instances of text surrounded by hash characters. The shift() method removes and returns the first element from an array, making it ...

Read More

What is the "get" keyword before a function in a class - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 505 Views

The get keyword in JavaScript creates a getter method that allows you to access a function like a property. When you call the getter, it executes the function and returns its value without using parentheses. Syntax class ClassName { get propertyName() { // return some value } } Basic Example Here's how to define and use a getter method: class Employee { constructor(name) { ...

Read More

Get the correct century from 2-digit year date value - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 375 Views

When working with 2-digit year values, you need to determine which century they belong to. A common approach is using a pivot year to decide between 19XX and 20XX centuries. Example Following is the code − const yearRangeValue = 18; const getCorrectCentury = dateValues => { var [date, month, year] = dateValues.split("-"); var originalYear = +year > yearRangeValue ? "19" + year : "20" + year; return new Date(originalYear + "-" + month + "-" + date).toLocaleDateString('en-GB') }; console.log(getCorrectCentury('10-JAN-19')); console.log(getCorrectCentury('10-JAN-17')); console.log(getCorrectCentury('10-JAN-25')); ...

Read More

How to merge specific elements inside an array together - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 164 Views

When working with arrays containing mixed data types, you might need to merge consecutive numeric elements while keeping certain separators intact. This is common when processing data that represents grouped numbers. Let's say we have the following array: var values = [7, 5, 3, 8, 9, '/', 9, 5, 8, 2, '/', 3, 4, 8]; console.log("Original array:", values); Original array: [7, 5, 3, 8, 9, '/', 9, 5, 8, 2, '/', 3, 4, 8] Using join() and split() Method To merge specific elements while preserving separators, we can use join(), split(), ...

Read More

Wrap object properties of type string with arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 586 Views

When working with objects, you may need to ensure all properties are arrays. This is useful for normalizing data structures where some properties might be strings while others are already arrays. The Problem Consider an object where some properties are strings and others are arrays. To process them uniformly, you need all properties to be arrays: var details = { name: ["John", "David"], age1: "21", age2: "23" }; console.log("Original object:"); console.log(details); Original object: { name: [ ...

Read More

Array filtering using first string letter in JavaScript

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

Suppose we have an array that contains names of some people like this: const arr = ['Amy', 'Dolly', 'Jason', 'Madison', 'Patricia']; console.log(arr); [ 'Amy', 'Dolly', 'Jason', 'Madison', 'Patricia' ] We are required to write a JavaScript function that takes in one such array as the first argument, and two lowercase alphabet characters as second and third arguments. Then, our function should filter the array to contain only those elements that start with alphabets that fall within the range specified by the second and third arguments. Therefore, if the second and third arguments ...

Read More

Number of vowels within an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 790 Views

We are required to write a JavaScript function that takes in an array of strings, (they may be a single character or greater than that). Our function should simply count all the vowels contained in the array. Example Let us write the code − const arr = ['Amy', 'Dolly', 'Jason', 'Madison', 'Patricia']; const countVowels = (arr = []) => { const legend = 'aeiou'; const isVowel = c => legend.includes(c.toLowerCase()); let count = 0; arr.forEach(el => { for(let ...

Read More

Comparing objects in JavaScript and return array of common keys having common values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 817 Views

We are required to write a JavaScript function that takes in two objects. The function should return an array of all those common keys that have common values across both objects. Example The code for this will be − const obj1 = { a: true, b: false, c: "foo" }; const obj2 = { a: false, b: false, c: "foo" }; const compareObjects = (obj1 = {}, obj2 = {}) => { const common = Object.keys(obj1).filter(key => { if(obj1[key] === obj2[key] && obj2.hasOwnProperty(key)){ ...

Read More

Dividing a floating number, rounding it to 2 decimals, and calculating the remainder in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 892 Views

When dividing a floating-point number, you often need to round the result to a specific number of decimal places and handle any remainder. This is useful for financial calculations, splitting costs, or distributing values evenly. Problem Suppose we have a floating-point number: 2.74 If we divide this number by 4, the result is 0.685. We want to divide this number by 4 but the result should be rounded to 2 decimals. Therefore, the result should be: 3 times 0.69 and a remainder of 0.67 Solution Using toFixed() The ...

Read More

Converting numbers to Indian currency using JavaScript

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

Converting numbers to Indian currency format is a common requirement in web applications. JavaScript provides a built-in method to format numbers according to different locales, including the Indian numbering system. Understanding Indian Currency Format Indian currency uses a unique grouping system where numbers are grouped by hundreds (lakhs and crores) rather than thousands. For example: 1000 → ₹1, 000.00 129943 → ₹1, 29, 943.00 76768798 → ₹7, 67, 68, 798.00 Using toLocaleString() Method The toLocaleString() method with Indian locale ('en-IN') automatically handles the currency formatting: const num1 = 1000; const num2 ...

Read More
Showing 121–130 of 5,881 articles
« Prev 1 11 12 13 14 15 589 Next »
Advertisements