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 69 of 534
How 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 MorePicking the odd one out in JavaScript
We are required to write a JavaScript function that takes in an array of literals that contains all similar elements but one. Our function should return the unlike number. Therefore, let's write the code for this function − Example The code for this will be − const arr = [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]; // considering that the length of array is at least 3 const findUnlike = arr => { for(let i = 1; i < arr.length-1; i++){ ...
Read MoreDeleting the last vowel from a string in JavaScript
We are required to write a JavaScript function that takes in a string and returns a new string with the last vowel of each word removed. For example: If the string is − const str = 'This is an example string'; Then the output should be − const output = 'Ths s n exampl strng'; Therefore, let's write the code for this function − Example The code for this will be − const str = 'This is an example string'; const removeLast = word => { ...
Read MoreSorting the numbers from within in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and reorders the digits of all the numbers internally in a specific order (let's say in ascending order for the sake of this problem). For example: If the array is − const arr = [543, 65, 343, 75, 567, 878, 87]; Then the output should be − const output = [345, 56, 334, 57, 567, 788, 78]; Therefore, let's write the code for this function − Example The code for this will be − ...
Read MoreConstructing array from string unique characters in JavaScript
We are required to write a JavaScript function that takes in a string and starts mapping its characters from 0. And every time, the function encounters a unique (non-duplicate) character it should increase the mapping count by 1 otherwise it should map the same number for duplicate characters. For example: If the string is − const str = 'heeeyyyy'; Then the output should be − const output = [0, 1, 1, 1, 2, 2, 2, 2]; Therefore, let's write the code for this function − Example The code ...
Read MoreFinding pandigital numbers using JavaScript
A pandigital number is a number that contains all digits (0-9) at least once. In this tutorial, we'll create a JavaScript function to check if a given number string is pandigital. What is a Pandigital Number? A pandigital number must contain every digit from 0 to 9 at least once. For example, "1234567890" is pandigital, while "123456789" is not (missing 0). Example Let's implement a function to check if a number string is pandigital: const numStr1 = '47458892414'; const numStr2 = '53657687691428890'; const isPandigital = numStr => { let ...
Read More