Javascript Articles

Page 485 of 534

Check if a string is sorted in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 580 Views

We are required to write a JavaScript function that takes in a string and checks whether it is sorted or not. A string is considered sorted if its characters are arranged in either ascending or descending order. For example: isSorted('adefgjmxz') // true (ascending) isSorted('zxmfdba') // true (descending) isSorted('dsfdsfva') // false (mixed order) Method 1: Character by Character Comparison This approach compares adjacent characters and tracks whether the string is ascending or descending: const str = 'abdfhlmxz'; const findDiff = (a, b) => a.charCodeAt(0) - b.charCodeAt(0); ...

Read More

Creating an array using a string which contains the key and the value of the properties - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 125 Views

Suppose, we have a special kind of string like this − const str = "Integer, 1 Float, 2.0Boolean, True Integer, 6Float, 3.66 Boolean, False"; We are required to write a JavaScript function that converts the above string into an array of objects, using the String.prototype.split() method − const arr = [ { "Integer": 1, "Float": 2.0 }, { ...

Read More

Figuring out the highest value through a for in loop - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 166 Views

When working with comma-separated strings in JavaScript, you might need to find which value appears most frequently. This tutorial demonstrates how to use a for-in loop to count occurrences and determine the most frequent item. Suppose we have a comma-separated string containing fruit names: const str = 'Banana, Banana, Pear, Orange, Apple, Melon, Grape, Apple, Banana, Grape, Melon, Grape, Melon, Apple, Grape, Banana, Orange, Melon, Orange, Banana, Banana, Orange, Pear, Grape, Orange, Orange, Apple, Apple, Banana'; console.log(str); Banana, Banana, Pear, Orange, Apple, Melon, Grape, Apple, Banana, Grape, Melon, Grape, Melon, Apple, Grape, Banana, Orange, ...

Read More

Alternatively merging two arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 836 Views

We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays. For example, if the two arrays are: const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3]; Then the output should be: const output = [4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3]; Method 1: Using Index-Based Logic This approach uses a single loop and alternates between arrays based on even/odd ...

Read More

Sum all similar elements in one array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 461 Views

We are required to write a JavaScript function that takes in an array of numbers and sums all the identical numbers together at one index, removing duplicates while preserving the first occurrence position. For example, if we have repeated numbers in an array, we want to combine them into a single sum at the position where they first appear. Problem Statement If the input array is: const arr = [20, 10, 15, 20, 15, 10]; Then the output should be: const output = [40, 20, 30]; Here, 20 appears ...

Read More

Find the shortest string in an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 778 Views

We are required to write a JavaScript function that takes in an array of strings and returns the shortest string in the array. This is a common programming problem that can be solved efficiently using different approaches. We will explore multiple methods to find the shortest string, from simple loops to functional programming approaches. Using for Loop The most straightforward approach uses a for loop to iterate through the array and track the shortest string: const arr = ['this', 'can', 'be', 'some', 'random', 'sentence']; function findShortestString(strings) { if (strings.length === ...

Read More

Retrieve user id from array of object - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 959 Views

Suppose, we have an array of objects where the user names are mapped to some unique ids like this − const arr = [ {"4": "Rahul"}, {"7": "Vikram"}, {"6": "Rahul"}, {"3": "Aakash"}, {"5": "Vikram"} ]; As apparent in the array, same names can have more than one ids but same ids cannot be used to map two different names. We are required to write a JavaScript function that takes in one such array as the first argument and a name ...

Read More

JavaScript: Finding nearest prime number greater or equal to sum of digits - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 215 Views

We are required to write a JavaScript function that takes in a number, finds the sum of its digits and returns a prime number that is just greater than or equal to the sum. Understanding the Problem For a number like 56563, we first calculate the sum of digits: 5 + 6 + 5 + 6 + 3 = 25. Since 25 is not prime (divisible by 5), we find the next prime number, which is 29. Example const num = 56563; const digitSum = (num, sum = 0) => { ...

Read More

Finding special type of numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 229 Views

In the decimal number system, all the real numbers can be divided into two groups: Rational Numbers Irrational Numbers For the scope of this problem we will only discuss the rational numbers. All those numbers which can be written in the p/q form (where q ≠ 0) are called rational numbers, like 14, 4.6, 3.33333... and many more. The rational numbers can further be divided into two groups: Terminating decimal numbers Repeating decimal numbers This categorization is made on the basis ...

Read More

Are array of numbers equal - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 199 Views

We need to write a JavaScript function that takes in two arrays of numbers and checks for their equality based on specific criteria. The arrays will be considered equal if: They contain the same elements and in the same order. The product of all the elements of the first array and second array is equal. Example Arrays The first array of numbers: const first = [3, 5, 6, 7, 7]; The second array of numbers: const second = [7, 5, 3, 7, 6]; ...

Read More
Showing 4841–4850 of 5,340 articles
« Prev 1 483 484 485 486 487 534 Next »
Advertisements