Javascript Articles

Page 480 of 534

Find the primorial of numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 371 Views

The primorial of a number n is equal to the product of the first n prime numbers. This is different from factorial, which multiplies consecutive integers. For example, if n = 4, we need the first 4 prime numbers: 2, 3, 5, and 7. 2 × 3 × 5 × 7 = 210 Let's write a JavaScript function that calculates the primorial of a given number. Understanding Prime Numbers First, we need a helper function to check if a number is prime: const isPrime = n => { ...

Read More

Finding the nth day from today - JavaScript (JS Date)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 522 Views

We are required to write a JavaScript function that takes in a number n as the only input. The function should first find the current day (using Date Object in JavaScript) and then the function should return the day n days from today. For example − If today is Monday and n = 2, Then the output should be − Wednesday Understanding the Problem JavaScript's Date.getDay() returns 0 for Sunday, 1 for Monday, and so on. We need to handle the modulo operation correctly to cycle through weekdays. Method 1: ...

Read More

Checking the intensity of shuffle of an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 220 Views

An array of numbers is 100% shuffled if no two consecutive numbers appear together in ascending order. It is 0% shuffled if all adjacent pairs are in ascending order. The shuffle intensity measures how much an array deviates from a perfectly sorted ascending sequence. For an array of length n, there are n-1 adjacent pairs to examine. We calculate the percentage of pairs that are NOT in ascending order. How It Works The algorithm counts pairs where the first element is greater than the second element (descending pairs). The shuffle intensity is calculated as: Shuffle ...

Read More

Finding unlike number in an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 244 Views

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 More

Checking progressive array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 240 Views

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 More

Checking semiprime numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 432 Views

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 More

Mapping unique characters of string to an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 474 Views

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 More

Returning the second most frequent character from a string (including spaces) - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 444 Views

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 More

Reversing the prime length words - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 233 Views

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 More

Resolving or rejecting promises accordingly - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 145 Views

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 More
Showing 4791–4800 of 5,340 articles
« Prev 1 478 479 480 481 482 534 Next »
Advertisements