Javascript Articles

Page 69 of 534

How many times can we sum number digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 180 Views

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 More

Return a map representing the frequency of each data type in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 156 Views

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

Pairing an array from extreme ends in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 223 Views

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 More

Generating random string of specified length in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 507 Views

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 More

Finding nearest prime to a specified number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 288 Views

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

Picking the odd one out in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 598 Views

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 More

Deleting the last vowel from a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 282 Views

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 More

Sorting the numbers from within in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 188 Views

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 More

Constructing array from string unique characters in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 184 Views

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 More

Finding pandigital numbers using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 227 Views

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
Showing 681–690 of 5,340 articles
« Prev 1 67 68 69 70 71 534 Next »
Advertisements