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 179 of 589
Finding unlike number in an array - JavaScript
We are required to write a JavaScript function that takes in an array of literals containing all similar elements but one. Our function should return the unlike number. The Problem Given an array where all elements are the same except one, we need to identify and return the unique element. This is a common programming challenge that can be solved efficiently using different approaches. Method 1: Using Frequency Count The most straightforward approach is to count the frequency of each element and return the one that appears only once. const arr = [2, 4, ...
Read MoreChecking progressive array - JavaScript
We are required to write a JavaScript function that takes in an array of strings, ordered by ascending length. The function should return true if, for each pair of consecutive strings, the second string can be formed from the first by adding a single letter either at the beginning or end. For example: If the array is given by − const arr = ["c", "ca", "can", "acan", "acane", "dacane"]; Then our function should return true. How It Works The algorithm checks each consecutive pair of strings to verify that the longer string ...
Read MoreChecking semiprime numbers - JavaScript
We are required to write a JavaScript function that takes in a number and determines if the provided number is a semiprime or not. What is a Semiprime? A semiprime number is a special type of composite number that is the product of exactly two prime numbers (not necessarily distinct). For example: 6 = 2 × 3 (product of two distinct primes) 15 = 3 × 5 (product of two distinct primes) 10 = 2 × 5 (product of two distinct primes) 4 = 2 × 2 (square of a prime) 9 = 3 × 3 ...
Read MoreMapping unique characters of string to an array - JavaScript
We are required to write a JavaScript function that takes in a string and starts mapping its characters from 0. Every time the function encounters a unique (non-duplicate) character, it should increase the mapping count by 1, otherwise 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]; How It Works The algorithm works by tracking consecutive character groups. When a character differs from the previous one, ...
Read MoreReturning the second most frequent character from a string (including spaces) - JavaScript
We are required to write a JavaScript function that takes in a string and returns the character which makes second most appearances in the string. Approach The solution involves counting character frequencies, sorting them by frequency, and returning the second most frequent character. Here's how it works: Create a frequency map to count each character occurrence Convert the map to an array of [character, frequency] pairs Sort the array by frequency in descending order Return the character at index 1 (second position) ...
Read MoreReversing the prime length words - JavaScript
We are required to write a JavaScript function that takes in a string that contains words joined by whitespaces. Our function should create a new string that has all the words from the original string, but words whose length is a prime number should be reversed (i.e., words with length 2, 3, 5, 7, 11, etc.). What are Prime Numbers? A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example: 2, 3, 5, 7, 11, 13, etc. Algorithm Our approach involves three main steps: ...
Read MoreResolving or rejecting promises accordingly - JavaScript
We are required to write a JavaScript function that imitates a network request, for that we can use the JavaScript setTimeout() function, that executes a task after a given time interval. Our function should return a promise that resolves when the request takes place successfully, otherwise it rejects Understanding Promise Resolution and Rejection When creating promises, we use the Promise constructor which takes an executor function with two parameters: resolve and reject. The resolve function is called when the operation succeeds, while reject is called when it fails. Example Following is the code that demonstrates ...
Read MoreFiltering out primes from an array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns a new filtered array that does not contain any prime number. Problem Statement Given an array of numbers, we need to filter out all prime numbers and return only the composite and non-prime numbers. const arr = [34, 56, 3, 56, 4, 343, 68, 56, 34, 87, 8, 45, 34]; Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Numbers like 2, ...
Read MoreFinding lunar sum of Numbers - JavaScript
The concept of lunar sum says that the sum of two numbers is calculated by taking the larger digit from each corresponding position, instead of adding them together. How Lunar Sum Works For each digit position, we compare the digits from both numbers and take the maximum value. Let's see how this works with an example: a = 879 and b = 768 Position-wise comparison: Position 1: max(8, 7) = 8 Position 2: max(7, 6) = 7 Position 3: max(9, 8) = 9 Lunar Sum: 879 Implementation ...
Read MoreFinding average word length of sentences - JavaScript
We are required to write a JavaScript function that takes in a string of words joined by whitespaces. The function should calculate and return the average length of all the words present in the string rounded to two decimal places. Understanding the Problem To find the average word length, we need to: Split the string into individual words Calculate the total length of all words (excluding spaces) Divide by the number of words Round the result to two decimal places Example Following ...
Read More