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 175 of 589
Program to find uncommon elements in two arrays - JavaScript
Finding uncommon elements in two arrays means identifying elements that exist in one array but not in both. This is also known as finding the symmetric difference between two arrays. Problem Definition Given two arrays, we need to find elements that are present in either the first array or the second array, but not in both arrays. const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; console.log("Array 1:", arr1); console.log("Array 2:", arr2); Array 1: [12, 54, 2, 4, 6, 34, ...
Read MoreCount appearances of a string in another - JavaScript
We are required to write a JavaScript function that takes in two strings and returns the count of the number of times the first string appears in the second string. Let's say our string is: const main = 'This is the is main is string'; We have to find the appearance of the below string in the above "main" string: const sub = 'is'; Using Regular Expression with replace() Let's write the code for this function using a regular expression approach: const main = 'This is the is ...
Read MoreProgram to test the equality of two arrays - JavaScript
We are required to write a JavaScript function that takes in two arrays of literals and checks the corresponding elements of the array and it should return true if all the corresponding elements of the array are equal otherwise it should return false. Let's write the code for this function − Example Following is the code − const arr1 = [1, 4, 5, 3, 5, 6]; const arr2 = [1, 4, 5, 2, 5, 6]; const areEqual = (first, second) => { if(first.length !== second.length){ ...
Read MoreGetting tomorrow and day after tomorrow date in JavaScript
Using the Date class in JavaScript, we can easily calculate tomorrow and the day after tomorrow from the current date. The new Date() constructor returns a JavaScript Date object for the current day. Getting Today's Date First, let's get today's date using the Date constructor: const today = new Date(); console.log("Today:", today.toDateString()); Today: Thu Aug 13 2020 Method 1: Using setDate() with Date Copying We can create new Date objects by copying today's date and then adding days using the setDate() method: // Getting today's date const today ...
Read MoreFinding least number of notes to sum an amount - JavaScript
Suppose, we have a currency system where we have denominations of 1000 units, 500 units, 100 units, 50 units, 20 units, 10 units, 5 units, 2 units and 1 unit. Given a specific amount, we are required to write a function that calculates the least number of total denominations that sum up to the amount. For example, if the amount is 512, The least number of notes that will add up to it will be: 1 unit of 500, 1 unit of 10 and 1 unit of 2. So, for 512, our function should ...
Read MoreComparing ascii scores of strings - JavaScript
ASCII is a 7-bit character encoding standard where every character has a unique decimal code. In JavaScript, we can use the charCodeAt() method to get the ASCII value of any character. This article demonstrates how to compare two strings by calculating their ASCII scores (the sum of ASCII values of all characters) and finding the difference between them. Understanding ASCII Values Each character has a specific ASCII decimal value: console.log('A'.charCodeAt(0)); // 65 console.log('a'.charCodeAt(0)); // 97 console.log(' '.charCodeAt(0)); // 32 (space) console.log('1'.charCodeAt(0)); // 49 65 97 32 49 ...
Read MoreConvert nested array to string - JavaScript
Converting a nested array to a string in JavaScript involves flattening all nested elements and concatenating their values. This is commonly needed when processing complex data structures. Problem Statement We need to write a JavaScript function that takes a nested array of literals and converts it to a single string by concatenating all values, regardless of nesting depth. const arr = [ 'hello', [ 'world', 'how', [ 'are', 'you', [ ...
Read MoreConvert object to a Map - JavaScript
In JavaScript, you can convert an object to a Map using several approaches. Maps offer better performance for frequent additions and deletions compared to objects. Sample Object Let's start with this example object: const obj = { name: "Vikas", age: 45, occupation: "Frontend Developer", address: "Tilak Nagar, New Delhi", experience: 23, salary: "98000" }; Method 1: Using Object.entries() (Recommended) The most concise approach uses Object.entries() which returns key-value pairs ...
Read MoreAlternate addition multiplication in an array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns the alternative multiplicative sum of the elements. For example, if the array is: const arr = [1, 2, 4, 1, 2, 3, 4, 3]; Then the output should be calculated like this: 1*2 + 4*1 + 2*3 + 4*3 2 + 4 + 6 + 12 And the final output should be: 24 How It Works The algorithm pairs consecutive elements starting from index 0. Each pair is multiplied ...
Read MoreSwitch case calculator in JavaScript
Let's say, we are required to write a JavaScript function that takes in a string like these to create a calculator − "4 add 6" "6 divide 7" "23 modulo 8" Basically, the idea is that the string will contain two numbers on either sides and a string representing the operation in the middle. The string in the middle can take one of these five values − "add", "divide", "multiply", "modulo", "subtract" Our job is to return the correct result based on the string Syntax switch (expression) { ...
Read More