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
Javascript Articles
Page 103 of 534
Removing first k characters from string in JavaScript
We are required to write a JavaScript function that takes in a string and a number, say k and returns another string with first k characters removed from the string. For example: If the original string is − const str = "this is a string" and, n = 4 then the output should be − const output = " is a string" Method 1: Using substr() Method The substr() method extracts characters from a string starting at a specified position. const str = 'this ...
Read MorePrime numbers within a range in JavaScript
We are required to write a JavaScript function that takes in two numbers, say, a and b and returns the total number of prime numbers between a and b (including a and b, if they are prime). For example: If a = 21, and b = 38. The prime numbers between them are 23, 29, 31, 37 And their count is 4 Our function should return 4 Understanding Prime Numbers A prime number is a natural number greater than 1 that has ...
Read MoreASCII sum difference of strings in JavaScript
ASCII (American Standard Code for Information Interchange) is a 7-bit character encoding standard where every character has a unique decimal code. We can calculate the ASCII sum of strings and find their difference in JavaScript. Understanding ASCII Codes Each character has a specific ASCII value. For example, 'A' = 65, 'a' = 97, '0' = 48, and space = 32. We can get these values using JavaScript's charCodeAt() method. Getting ASCII Values console.log('A'.charCodeAt(0)); // 65 console.log('a'.charCodeAt(0)); // 97 console.log('0'.charCodeAt(0)); // 48 console.log(' '.charCodeAt(0)); ...
Read MoreObject to Map conversion in JavaScript
In JavaScript, you can convert an object to a Map using several approaches. Maps offer advantages over objects like guaranteed key insertion order and the ability to use any data type as keys. Suppose we have an object like this: const obj = { name: "Jai", age: 32, occupation: "Software Engineer", address: "Dhindosh, Maharashtra", salary: "146000" }; We need to convert this object into a Map while preserving all key-value pairs. Using Object.entries() (Recommended) The most concise approach uses Object.entries() with the Map ...
Read MoreAddition multiplication ladder in an array in 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, 3, 4, 5, 6, 7]; Then the output should be calculated like this − 1*2+3*4+5*6+7 2+12+30+7 And the output should be − 51 Let's write the code for this function − How It Works The algorithm pairs elements at even indices with their next element, multiplies them, and adds ...
Read MoreMerge and remove duplicates in JavaScript Array
Merging arrays and removing duplicates is a common task in JavaScript. There are several approaches to accomplish this, from traditional loops to modern ES6 methods. Problem Statement Given two arrays of numbers, we need to combine them into a single array where each element appears only once. const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6]; console.log("Array 1:", arr1); console.log("Array 2:", arr2); Array 1: [ 2, 4, 5, 3, 7, 8, 9 ] Array 2: [ 1, 4, 5, 2, 3, 7, ...
Read MoreFiltering out the non-unique value to appear only once in JavaScript
We have an array that contains some duplicate values appearing multiple times. We need to extract only the elements that appear more than once in the array, but show each duplicate element only once in the result. const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4]; console.log("Original array:", arr); Original array: [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4] We need to write a JavaScript function that filters out elements that appear multiple times and returns each duplicate element only once. For ...
Read MoreChecking for the similarity of two 2-D arrays in JavaScript
We are required to write a JavaScript function that takes in two 2-D arrays and returns a boolean based on the check whether the arrays are equal or not. The equality of these arrays, in our case, is determined by the equality of corresponding elements. Both the arrays should have same number of rows and columns. Also, arr1[i][j] === arr2[i][j] should yield true for all i between [0, number of rows] and j between [0, number of columns] Method 1: Basic Nested Loop Approach This method compares each element position by position using nested loops: ...
Read MoreSorting an array of literals using quick sort in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and uses the quick sort algorithm to sort it. QuickSort Algorithm QuickSort is a divide-and-conquer algorithm that works by selecting a 'pivot' element from the array and partitioning other elements into two sub-arrays according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively. How QuickSort Works The algorithm follows these steps: Choose a pivot element from the array (typically the middle element) Partition the ...
Read MoreAlternate casing a string in JavaScript
We are required to write a JavaScript function that takes in a string and constructs a new string with all the uppercase characters from the original string converted to lowercase and all the lowercase characters converted to uppercase from the original string. For example: If the string is − const str = 'The Case OF tHis StrinG Will Be FLiPped'; Expected Output Then the output should be − const output = 'tHE cASE of ThIS sTRINg wILL bE flIpPED'; Using Manual Character Code Manipulation This approach manually checks character ...
Read More