Javascript Articles

Page 135 of 534

Alphanumeric sorting using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 462 Views

We have a mixed array that we need to sort by alphabet and then by digit. This requires a custom sorting function that handles both alphabetical and numerical parts correctly. const arr = ['Ab-1', 'Ab-11', 'Ab-12', 'ab-10', 'ab-100', 'ab-101', 'ab2', 'ab-3', 'ab-105']; console.log("Original array:", arr); Original array: [ 'Ab-1', 'Ab-11', 'Ab-12', 'ab-10', 'ab-100', 'ab-101', 'ab2', 'ab-3', 'ab-105' ] The Problem with Standard Sort JavaScript's default string sort doesn't handle numbers correctly. It treats "10" as smaller than "2" because it compares character ...

Read More

Enter a number and write a function that adds the digits together on button click in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 660 Views

We are required to write a JavaScript program that provides users an input to fill in a number. And upon filling when the user clicks the button, we should display the sum of all the digits of the number. HTML Structure First, let's create the HTML elements needed for our application: Submit JavaScript Implementation Here's the JavaScript function that calculates the sum of digits: function myFunc() { var num = document.getElementById('digits').value; var tot = 0; ...

Read More

JavaScript - summing numbers from strings nested in array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 224 Views

When working with arrays containing string numbers (like credit card numbers), you often need to extract and sum the numeric values. This article shows how to find the string with the greatest sum of digits. Problem Statement Given an array of credit card numbers as strings, we need to find the card with the highest sum of digits. If multiple cards have the same sum, return the last one encountered. const arr = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260']; Solution Approach The solution involves two main steps: extracting numbers from each string and calculating their ...

Read More

Longest decreasing subsequence subarray in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 263 Views

We are required to write a JavaScript function that takes in an array of integers and returns the length of the longest consecutive decreasing subsequence (subarray) from the array. Problem Statement Given an array of integers, find the length of the longest contiguous subsequence where each element is smaller than the previous one. For example, if the input array is: const arr = [5, 2, 5, 4, 3, 2, 4, 6, 7]; The longest decreasing subsequence is [5, 4, 3, 2] with length 4. Solution Approach We'll track the current decreasing ...

Read More

Fuzzy Search Algorithm in JavaScript

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

Fuzzy search allows finding strings that match a pattern even when characters are not consecutive. In JavaScript, we can implement a fuzzy search algorithm that checks if characters from a search query appear in the same order within a target string. How Fuzzy Search Works The algorithm loops through each character of the search query and verifies that all characters exist in the target string in the same sequential order, though they don't need to be adjacent. For example: ('a haystack with a needle').fuzzySearch('hay sucks'); // false ('a haystack with a needle').fuzzySearch('sack hand'); // true ...

Read More

Modify an array based on another array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 998 Views

When working with arrays in JavaScript, you might need to modify one array based on patterns or elements found in another array. This article demonstrates how to join consecutive elements from a reference array when they appear as combined phrases in a second array. Problem Statement Suppose we have a reference array of individual words: const reference = ["your", "majesty", "they", "are", "ready"]; And we want to modify it based on another array that contains some words joined together: const another = ["your", "they are"]; The goal is to create ...

Read More

Calculating the sum of digits of factorial JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 543 Views

We are required to write a JavaScript function that takes in a number. The function should first calculate the factorial of that number and then it should return the sum of the digits of the calculated factorial. For example, for the number 6, the factorial will be 720, so the sum of digits (7 + 2 + 0) should be 9. Understanding the Problem This problem involves two steps: Calculate the factorial of a given number Sum all digits in the factorial result Step 1: Calculate ...

Read More

Insert a number into a sorted array of numbers JavaScript

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

We are required to write a JavaScript function that takes in a sorted array of numbers as the first argument and a single number as the second argument. The function should push the number specified as the second argument into the array without distorting the sorting of the elements. We are required to do this without creating another array. Approach: Binary Search with In-Place Insertion The solution uses binary search to find the correct insertion position, then shifts elements using a swapping technique to maintain order without extra space. Example const arr = ...

Read More

Check if a string is entirely made of the same substring JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 413 Views

In JavaScript, you can check if a string is entirely made of repeated substrings using various approaches. This is useful for pattern validation and string analysis. The problem requires that the string consists of a repeated character sequence with at least one repetition. For example, "aa" contains two "a" substrings, "abcabcabc" contains three "abc" substrings, but "ababa" fails because it has an extra character. Examples of Valid and Invalid Patterns "aa" should return true because it entirely contains two strings "a" "aaa" should return true because it entirely ...

Read More

Compare Strings in JavaScript and return percentage of likeliness

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

We are required to write a JavaScript function that can compare two strings and return the percentage likeliness of how much they are alike. The percentage will be nothing but a measure of many characters the two strings have in common. If they are completely similar the output should be 100, and if they contain no common character at all, the output should be 0. Understanding the Algorithm This implementation uses the Levenshtein distance algorithm to calculate string similarity. The Levenshtein distance measures the minimum number of single-character edits (insertions, deletions, or substitutions) needed to change one ...

Read More
Showing 1341–1350 of 5,340 articles
« Prev 1 133 134 135 136 137 534 Next »
Advertisements