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
Web Development Articles
Page 167 of 801
Keeping 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 MoreHow many times can we sum number digits in JavaScript
We are required to write a JavaScript function that takes in a positive integer and returns its additive persistence. The additive persistence of an integer, say n, is the number of times we have to replace the number with the sum of its digits until the number becomes a single digit integer. Example Walkthrough For example: If the number is: 1679583 Then we calculate: 1 + 6 + 7 + 9 + 5 + 8 + 3 = 39 // Pass 1 3 + 9 = 12 ...
Read MoreReturn a map representing the frequency of each data type in an array in JavaScript
We are required to write a JavaScript function that takes in an array that contains elements of different data types and the function should return a map representing the frequency of each data type. Let's say the following is our array − const arr = [23, 'df', undefined, null, 12, { name: 'Rajesh' }, [2, 4, 7], 'dfd', null, Symbol('*'), 8]; Understanding the Problem We need to count how many times each JavaScript data type appears in the array. JavaScript's typeof operator will help us identify the data type of each ...
Read More