Web Development Articles

Page 463 of 801

Find longest string in array (excluding spaces) JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 237 Views

We are required to write a function that accepts an array of string literals and returns the index of the longest string in the array. While calculating the length of strings we don't have to consider the length occupied by whitespaces. If two or more strings have the same longest length, we have to return the index of the first string that does so. We will iterate over the array, split each element by whitespace, join again and calculate the length. We'll track the maximum length and its index, updating when we find a longer string. Syntax ...

Read More

Check Disarium number - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 447 Views

A Disarium number is a number where the sum of its digits raised to their respective positions equals the original number. Definition For a number with digits xy...z, it's a Disarium number if: xy...z = x^1 + y^2 + ... + z^n Where n is the total number of digits in the number. Example Let's check if 175 is a Disarium number: 175 = 1^1 + 7^2 + 5^3 = 1 + 49 + 125 = 175 Since the sum equals the original number, 175 is a Disarium ...

Read More

Sort array by year and month JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

Sorting arrays of objects by multiple criteria is a common requirement in JavaScript applications. In this tutorial, we'll learn how to sort an array by year first, then by month for objects with the same year. The Problem We have an array of objects containing year and month properties: const arr = [{ year: 2020, month: 'January' }, { year: 2017, month: 'March' }, { year: 2010, month: 'January' }, { ...

Read More

Count total punctuations in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 756 Views

In JavaScript, you can count punctuation marks in a string by checking each character against a set of punctuation symbols. Common punctuation marks include periods, commas, semicolons, exclamation marks, question marks, and quotation marks. Common Punctuation Characters '!', ", ", "'", ";", '"', ".", "-", "?" Example Here's a JavaScript function that counts punctuation marks in a string: const str = "This, is a-sentence;.Is this a sentence?"; const countPunctuation = str => { const punct = "!, ';".?-"; let count = 0; ...

Read More

Digital root sort algorithm JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 309 Views

Digital root of a positive integer is defined as the sum of all of its digits. We are given an array of integers and need to sort it based on digit root values. If a number comes before b, then the digit root of a should be less than or equal to the digit root of b. If two numbers have the same digit root, the smaller number should come first. For example, 4 and 13 have the same digit root (4 and 1+3=4), but since 4 < 13, the number 4 comes before 13 in the sorted array. ...

Read More

Remove all whitespaces from string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 315 Views

We are required to write a JavaScript function that takes in a string and returns a new string with all the characters of the original string but with whitespaces removed. Method 1: Using a For Loop This approach iterates through each character and builds a new string excluding spaces: const str = "This is an example string from which all whitespaces will be removed"; const removeWhitespaces = str => { let newStr = ''; for(let i = 0; i < str.length; i++){ ...

Read More

Returning an array with the minimum and maximum elements JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 203 Views

Finding the minimum and maximum elements in a JavaScript array is a common task. This tutorial shows how to return both values in a single array using different approaches. const arr = [12, 54, 6, 23, 87, 4, 545, 7, 65, 18, 87, 8, 76]; We need to write a function that finds the minimum and maximum elements and returns them as an array with minimum at index 0 and maximum at index 1. Using Array.reduce() Method The reduce() method provides an efficient way to traverse the array once and track both minimum and ...

Read More

Replacing upperCase and LowerCase in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 674 Views

We are required to write a JavaScript function that takes in a string and constructs a new string with all the uppercase characters converted to lowercase and all the lowercase characters converted to uppercase. Let's write the code for this function — Example Following is the code — const str = 'The Case OF tHis StrinG Will Be FLiPped'; const isUpperCase = char => char.charCodeAt(0) >= 65 && char.charCodeAt(0) char.charCodeAt(0) >= 97 && char.charCodeAt(0) { let newStr = ''; const margin = 32; ...

Read More

Fill missing numeric values in a JavaScript array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 591 Views

We are given an array of n entries, of which only 2 are Numbers, all other entries are null. Something like this: const arr = [null, null, -1, null, null, null, -3, null, null, null]; We are supposed to write a function that takes in this array and complete the arithmetic series of which these two numbers are a part. To understand this problem more clearly, we can think of these null values as blank space where we need to fill numbers so that the whole array forms an arithmetic progression. About Arithmetic Progression ...

Read More

Finding duplicate "words" in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 6K+ Views

We need to write a JavaScript function that takes a string and returns only the words that appear more than once in the original string. For example, if the input string is: const str = "big black bug bit a big black dog on his big black nose"; Then the output should be: "big black" Solution Using indexOf() and lastIndexOf() We can identify duplicates by comparing the first and last occurrence positions of each word: const str = "big black bug bit a big black dog on his ...

Read More
Showing 4621–4630 of 8,010 articles
« Prev 1 461 462 463 464 465 801 Next »
Advertisements