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
Object Oriented Programming Articles
Page 8 of 589
Changing 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 MorePairing an array from extreme ends in JavaScript
We are required to write a JavaScript function that takes in an array of Number / String literals and returns another array of arrays. With each subarray containing exactly two elements, the nth element from start and nth from last. For example: If the array is − const arr = [1, 2, 3, 4, 5, 6]; Then the output should be − [[1, 6], [2, 5], [3, 4]] Example The code for this will be − const arr = [1, 2, 3, 4, 5, 6]; const edgePairs ...
Read MoreGenerating random string of specified length in JavaScript
We are required to write a JavaScript function that takes in a number n and returns a random string of length n containing no other than the 26 English lowercase alphabets. Therefore, let's write the code for this function − Example The code for this will be − const num = 8; const randomNameGenerator = num => { let res = ''; for(let i = 0; i < num; i++){ const random = Math.floor(Math.random() * 26); ...
Read MoreFinding nearest prime to a specified number in JavaScript
We are required to write a JavaScript function that takes in a number and returns the first prime number that appears after n. For example: If the number is 24, then the output should be 29. Therefore, let's write the code for this function − Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples include 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, etc. Example The code for this will be − const num = ...
Read More