Javascript Articles

Page 481 of 534

Filtering out primes from an array - JavaScript

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

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 More

Finding lunar sum of Numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 355 Views

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 More

Finding average word length of sentences - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 515 Views

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

Adding a function for swapping cases to the prototype object of strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 181 Views

In JavaScript, we can extend built-in data types by adding custom methods to their prototype objects. This allows us to create reusable functions that work with existing data types like strings, arrays, and objects. In this tutorial, we'll create a custom swapCase() method for strings that swaps the case of each character - converting uppercase letters to lowercase and vice versa, while keeping non-alphabetic characters unchanged. Understanding String Prototype Extension When we add a method to String.prototype, it becomes available to all string instances. The this keyword inside the method refers to the string that called the ...

Read More

Sorting or Arranging an Array with standard array values - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 222 Views

We are required to sort a dynamic JavaScript array. The condition is that we are required to sort it according to the values stored in a particular order in a standard predefined array. Let's say the following is our dynamic array − const dbArray = ['Apple', 'Banana', 'Mango', 'Apple', 'Mango', 'Mango', 'Apple']; And suppose the standard array against which we have to sort the above array is like − const stdArray = ['Mango', 'Apple', 'Banana', 'Grapes']; So, after sorting the dbArray, my resultant array should look like − const ...

Read More

If ([] == false) is true, why does ([] || true) result in []? - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 586 Views

If we look closely at the problem statement, the difference between ([] == false) and ([] || true) is the following − In the first case, we are using loose conditional checking, allowing type coercion to take over. While in the second case, we are evaluating [] to its respective Boolean (truthy or falsy) which makes use of the function Boolean() instead of type coercion under the hood. Let's now unveil the conversions that happen behind the scenes in both cases. Case 1 - ([] == false) According to the MDN docs, when two data ...

Read More

How to access variables declared in a function, from another function using JavaScript?

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

In JavaScript, variables declared inside a function are scoped to that function and cannot be directly accessed from outside. However, there are several ways to make these variables available to other functions or globally. Problem with Function Scope Variables declared inside a function are private to that function: function myFunction() { let localVar = "I'm inside the function"; } myFunction(); // console.log(localVar); // This would cause an error Using Constructor Functions with 'this' You can use constructor functions to expose internal variables as properties: const num ...

Read More

Finding the intersection of arrays of strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 799 Views

We need to find the intersection of two arrays of strings and return an array containing the common elements. Each element in the result should appear as many times as it shows in both arrays. For example − If input is − arr1 = ['hello', 'world', 'how', 'are', 'you']; arr2 = ['hey', 'world', 'can', 'you', 'rotate']; Then the output should be − ['world', 'you'] Approach For unsorted arrays, we need to check every value of the first array against the second array. This approach has O(n²) time complexity. We ...

Read More

Dividing an array – JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 674 Views

Let's say, we are required to write a function that takes in an array arr of string / number literals as the first argument and a number n as second argument. We are required to return an array of n subarrays, each of which contains at most arr.length / n elements. And the distribution of elements should be like this − The first element goes in the first subarray, second in second, third in third and so on. Once we have one element in each subarray, we again start ...

Read More

Summing all the unique values of an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 335 Views

Let's say, we are required to write a JavaScript function that takes in an array of numbers with duplicate entries and sums all the duplicate entries to one index. For example − If the input array is − const input = [1, 3, 1, 3, 5, 7, 5, 4]; Then the output should be − const output = [2, 6, 10, 7, 4]; That means all the duplicate ones are summed to index 0 (1 + 1 = 2), all the duplicate threes are summed to index 1 (3 + ...

Read More
Showing 4801–4810 of 5,340 articles
« Prev 1 479 480 481 482 483 534 Next »
Advertisements