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 68 of 534
Checking for special type of Arrays in JavaScript
We are required to write a JavaScript function that takes in an array of literals and checks if elements are the same or not if read from front or back. Such arrays are also known by the name of palindrome arrays. Some examples of palindrome arrays are: const arr1 = ['a', 'b', 'c', 'b', 'a']; const arr2 = [4, 7, 7, 4]; const arr3 = [7, 7, 7, 7, 7, 7]; Method 1: Using a For Loop This approach compares elements from both ends moving towards the center: const arr = [1, ...
Read MoreFinding count of special characters in a string in JavaScript
In JavaScript, you can count special characters in a string by checking each character against a predefined set of special characters. This is useful for text validation, content analysis, or data processing tasks. Problem Statement We need to count occurrences of these special characters in a string: '!', ', ', ''', ';', '"', '.', '-', '?' The function should iterate through each character and increment a counter when it finds a match. Example Implementation const str = "This, is a-sentence;.Is this a sentence?"; const countSpecial = str => { ...
Read MoreKeeping only redundant words in a string in JavaScript
We are required to write a JavaScript function that takes in a string and returns a new string with only the words that appeared more than once in the original string. Problem Statement If the input string is: const str = 'this is a is this string that contains that some repeating words'; Then the output should be: 'this is that' The function should identify duplicate words and return them in the order they first appeared. Using indexOf() and lastIndexOf() This approach checks if a word's first occurrence ...
Read MoreRounding off numbers to some nearest power in JavaScript
We are required to write a JavaScript function that takes in a number and returns a number that can be represented as a power of 2 which is nearest to the input number. For example: If the input number is 145, the output should be 128 because 128 (which is 2^7) is the nearest power of 2 to 145. Understanding Powers of 2 Powers of 2 are numbers like: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512... Each number is double the previous one. Algorithm Approach The algorithm works by: ...
Read MoreCode to construct an object from a string in JavaScript
We are required to write a function that takes in a string as the first and the only argument and constructs an object with its keys based on the unique characters of the string and value of each key being defaulted to 0. For example: If the input string is − const str = 'hello world!'; Output Then the output object should be − const obj = { "h": 0, "e": 0, "l": 0, "o": 0, " ": 0, "w": 0, "r": 0, "d": 0, "!": 0 }; Using Array.reduce() ...
Read MoreChanging the case of a string using JavaScript
We are required to write a JavaScript function that takes in a string and converts it to snake case. Snake case is basically a style of writing strings by replacing the spaces with '_' and converting the first letter of each word to lowercase. Example The code for this will be − const str = 'This is a simple sentence'; const toSnakeCase = (str = '') => { const strArr = str.split(' '); const snakeArr = strArr.reduce((acc, val) => { ...
Read MoreVowel gaps array in JavaScript
We are required to write a JavaScript function that takes in a string with at least one vowel, and for each character in the string we have to map a number representing its nearest distance from a vowel. For example: If the string is − const str = 'vatghvf'; Output Then the output should be − const output = [1, 0, 1, 2, 3, 4, 5]; The logic is: 'v' is 1 position away from vowel 'a', 'a' is 0 (it's a vowel), 't' is 1 away from 'a', 'g' ...
Read MoreSwapping letter with succeeding alphabet in JavaScript
We are required to write a JavaScript function that takes in a string and changes every letter of the string from the English alphabets to its succeeding element. For example: If the string is − const str = 'how are you'; Output Then the output should be − const output = 'ipx bsf zpv' Therefore, let's write the code for this function − How It Works The algorithm converts each alphabetic character to its next letter in the alphabet. For 'z' and 'Z', it wraps around to 'a' ...
Read MoreThe n times dribbling strings in JavaScript
We are required to write a JavaScript function that takes in a string and a number, say n, and the function should return a new string in which all the letters of the original string are repeated n times. For example: If the string is − const str = 'how are you' And the number n is 2. Output Then the output should be − const output = 'hhooww aarree yyoouu' Therefore, let's write the code for this function − Using String.prototype.repeat() The most straightforward approach is to iterate through each ...
Read MoreSpecial type of sort of array of numbers in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and sorts the array such that first all the even numbers appear in ascending order and then all the odd numbers appear in ascending order. For example: If the input array is − const arr = [2, 5, 2, 6, 7, 1, 8, 9]; Expected Output Then the output should be − [2, 2, 6, 8, 1, 5, 7, 9] Using Custom Comparator Function We can solve this by creating a custom comparator function ...
Read More