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 9 of 589
Picking the odd one out in JavaScript
We are required to write a JavaScript function that takes in an array of literals that contains all similar elements but one. Our function should return the unlike number. Therefore, let's write the code for this function − Example The code for this will be − const arr = [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]; // considering that the length of array is at least 3 const findUnlike = arr => { for(let i = 1; i < arr.length-1; i++){ ...
Read MoreDeleting the last vowel from a string in JavaScript
We are required to write a JavaScript function that takes in a string and returns a new string with the last vowel of each word removed. For example: If the string is − const str = 'This is an example string'; Then the output should be − const output = 'Ths s n exampl strng'; Therefore, let's write the code for this function − Example The code for this will be − const str = 'This is an example string'; const removeLast = word => { ...
Read MoreSorting the numbers from within in 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]; Therefore, let's write the code for this function − Example The code for this will be − ...
Read MoreConstructing array from string unique characters in JavaScript
We are required to write a JavaScript function that takes in a string and starts mapping its characters from 0. And every time, the function encounters a unique (non-duplicate) character it should increase the mapping count by 1 otherwise it should map the same number for duplicate characters. For example: If the string is − const str = 'heeeyyyy'; Then the output should be − const output = [0, 1, 1, 1, 2, 2, 2, 2]; Therefore, let's write the code for this function − Example The code ...
Read MoreFinding pandigital numbers using JavaScript
A pandigital number is a number that contains all digits (0-9) at least once. In this tutorial, we'll create a JavaScript function to check if a given number string is pandigital. What is a Pandigital Number? A pandigital number must contain every digit from 0 to 9 at least once. For example, "1234567890" is pandigital, while "123456789" is not (missing 0). Example Let's implement a function to check if a number string is pandigital: const numStr1 = '47458892414'; const numStr2 = '53657687691428890'; const isPandigital = numStr => { let ...
Read MoreFinding sum of all unique elements in JavaScript
In JavaScript, you can sum duplicate elements by counting their occurrences and multiplying each unique value by its count. This technique is useful for consolidating arrays with repeated values. For example, if the input array is: const input = [1, 3, 1, 3, 5, 7, 5, 4]; The output should be: [2, 6, 10, 7, 4] This means: 1 appears 2 times (1×2=2), 3 appears 2 times (3×2=6), 5 appears 2 times (5×2=10), while 7 and 4 appear once each. Using Map to Count and Sum The most efficient ...
Read MoreDeviations in two JavaScript arrays in JavaScript
We have two arrays of numbers and need to find elements that exist in one array but not in both. This is called finding the symmetric difference or deviation between arrays. const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; We need to write a JavaScript function that takes two arrays and returns elements that are not common to both arrays. Using indexOf() Method The basic approach uses indexOf() to check if elements exist in the other array: const arr1 ...
Read MoreAND product of arrays in JavaScript
We have an array of arrays of boolean values like this: const arr = [[true, false, false], [false, false, false], [false, false, true]]; We are required to write a function that merges this array of arrays into a one-dimensional array by combining the corresponding elements of each subarray using the AND (&&) operator. Let's write the code for this function. We will be using Array.prototype.reduce() function to achieve this. Example The code for this will be: const arr = [[true, false, false], [false, false, false], [false, false, true]]; const andMerge ...
Read MoreKeeping only alphanumerals in a JavaScript string in JavaScript
We are required to write a JavaScript function that takes in a string that might contain some special characters. The function should return a new string that has all special characters replaced with their corresponding ASCII value, keeping only alphanumeric characters and spaces in their original form. Therefore, let's write the code for this function: Example The code for this will be: const str = 'Th!s !s @ str!ng th@t cont@!ns some special characters!!'; const specialToASCII = str => { let res = ''; for(let i ...
Read MoreDetecting the first non-unique element in array in JavaScript
We are required to write a function that returns the index of the very first element that appears at least twice in the array. If no element appears more than once, we have to return -1. We have to do this in constant space (i.e., without utilizing extra memory). So, let's write the solution for this problem. We will use a for loop to iterate over the array and use the Array.prototype.lastIndexOf() method to check for duplicacy. How It Works The algorithm works by comparing each element's current index with its last occurrence index using lastIndexOf(). ...
Read More